0

I'm new to android and i m creating application contact application I m trying to delete a row from table but its giving me nullpointerexception as shown in log below

this indicates that delete method is getting null value but through bundle i m getting the correct value and i m passing the same val in deketecontact method.

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) });
   }

log file

    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$1.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)
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
abhishek
  • 301
  • 1
  • 5
  • 29

1 Answers1

1

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

Because you are not initializing dbh object of databaseHelper class before calling deleteContact using dbh object. Initialize it before calling method:

dbh=new databaseHelper(...);
dbh.deleteContact(val);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213