0

I initial object db and called its method from my current class:

        db = new DBHelper(context);
        db.addCallerInfo(FragmentCaller.iName.getText().toString(),
                FragmentCaller.sex.getSelectedItemPosition(),
                FragmentCaller.iAddress.getText().toString(),
                FragmentCaller.iTel.getText().toString(),
                FragmentCaller.callerType.getSelectedItemPosition(),
                FragmentCaller.iSpecify.getText().toString());

In DBhelper class:

public void addCallerInfo(String name,int   sex ,String address,String  tel,int  callerType_id,String  specify) {
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("sex", sex);
        values.put("address", address);
        values.put("tel", tel);
        values.put("specify", specify);
        values.put("callerType_id", callerType_id);
        db.insert(TableHelper.TABLE_callerInfo, null, values);
    }

Then it has problem java.lang.NullPointerException:

Attempt to invoke virtual method long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues) on a null object reference

Help please! Thanks.

HassanUsman
  • 1,787
  • 1
  • 20
  • 38
Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39

2 Answers2

2

Add below line in addCallerInfo() method. Then you can insert contentValues into Data Base.

   SQLiteDatabase db = this.getWritableDatabase();
N J
  • 27,217
  • 13
  • 76
  • 96
Saritha G
  • 2,588
  • 1
  • 15
  • 27
-1

You can't declare the Database type Writable or Readable

First declare it then inserting values like this:

 SQLiteDatabase database = this.getWritableDatabase();

And For just Readable database

 SQLiteDatabase db = this.getReadableDatabase();
HassanUsman
  • 1,787
  • 1
  • 20
  • 38