尝试执行 databasehelper 的 delete 方法时出现 nullpointerexecption

nullpointerexecption when trying to execute delete method of databasehelper

我是 android 的新手,我正在创建应用程序联系人应用程序 我正在尝试从 table 中删除一行,但它给了我 nullpointerexception,如下面的日志所示

这表明 delete 方法正在获取空值,但通过 bundle 我获取了正确的值,并且我在 deketecontact 方法中传递了相同的值。

com.example.ViewRecord
public class ViewRecord extends Activity {
int val;
SQLiteDatabase sdb;
databaseHelper dbh;
//=new databaseHelper(getBaseContext(), "stud", null, 1);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_record);
    Bundle b = getIntent().getExtras();
    if(b!=null){
        String n=b.getString("pname").toUpperCase(Locale.ENGLISH);
        TextView t= (TextView) findViewById(R.id.tv1);
        t.setText("WELCOME "+ n);
        val = b.getInt("pid");

    }
    Button del= (Button) findViewById(R.id.button1);
    del.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(ViewRecord.this);
            builder.setMessage(R.string.deleteContact)
           .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  dbh.deleteContact(val);
                  Toast.makeText(getApplicationContext(), "Deleted Successfully",
                  Toast.LENGTH_SHORT).show();      
                  Intent intent = new Intent(getApplicationContext(),AddRecord.class);
                  startActivity(intent);
               }
            })
           .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  // User cancelled the dialog
               }
            });
            AlertDialog d = builder.create();
            d.setTitle("Are you sure");
            d.show();



        }
    });
}

}

  //and this is delete method in databasehelper.class
  public Integer deleteContact (Integer id)
   {
      Log.d("val1", ""+id);
      SQLiteDatabase db = this.getWritableDatabase();
     // db.delete("studrec", "id=", null);
      return db.delete("studrec", 
      "id = ? ",new String[] { Integer.toString(id) });
   }

日志文件

    01-10 19:19:00.680: E/AndroidRuntime(10261): FATAL EXCEPTION: main
01-10 19:19:00.680: E/AndroidRuntime(10261): Process: com.assign6, PID: 10261
01-10 19:19:00.680: E/AndroidRuntime(10261): java.lang.NullPointerException: Attempt to invoke                                   virtual method 'java.lang.Integer com.assign6.databaseHelper.deleteContact(java.lang.Integer)' on a null object reference
01-10 19:19:00.680: E/AndroidRuntime(10261):    at com.assign6.ViewRecord.onClick(ViewRecord.java:66)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.view.View.performClick(View.java:4756)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.view.View$PerformClick.run(View.java:19749)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.os.Handler.handleCallback(Handler.java:739)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.os.Looper.loop(Looper.java:135)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at android.app.ActivityThread.main(ActivityThread.java:5221)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at java.lang.reflect.Method.invoke(Native Method)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at java.lang.reflect.Method.invoke(Method.java:372)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-10 19:19:00.680: E/AndroidRuntime(10261):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

NullPointerException: Attempt to invoke virtual method 'java.lang.Integer com.example.databaseHelper.deleteContact

因为在使用 dbh 对象调用 deleteContact 之前,您没有初始化 databaseHelper class 的 dbh 对象。在调用方法之前初始化它:

dbh=new databaseHelper(...);
dbh.deleteContact(val);