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,485
  • 1
  • 17
  • 34
Lay Leangsros
  • 7,922
  • 6
  • 30
  • 37

2 Answers2

2

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

   SQLiteDatabase db = this.getWritableDatabase();
N J
  • 25,967
  • 13
  • 73
  • 94
Saritha G
  • 2,365
  • 1
  • 13
  • 25
-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,485
  • 1
  • 17
  • 34