0

I am having trouble submitting to the SQLite Database because it keeps bringing up an error of "Attempting to invoke a virtual method on a null object reference"

               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference

Am using a contract class that defines the database constants

    public class TableData {
public static abstract class TableInfo{

    public static final String USER_FIRSTNAME = "firstname";
    public static final String USER_LASTNAME = "lastname";
    public static final String USER_TABLE = "my_table";

}

}

I then describe my database in a MyDBHelper class

    public class MyDbHelper extends SQLiteOpenHelper{

private static final String DB_NAME = "USERNAMES.DB";
private static final int DB_VERSION = 1;
private static final String CREATE_QUERY = "CREATE TABLE "+TableData.TableInfo.USER_TABLE+" " +
        "("+TableData.TableInfo.USER_FIRSTNAME+" TEXT, "+TableData.TableInfo.USER_LASTNAME+" TEXT)";

public MyDbHelper(Context context) {
    super(context, DB_NAME , null, DB_VERSION);
    Log.e("DB OPERATIONS", "Database Created");
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_QUERY);
    Log.e("DB OPERATIONS", "Table Created");

}
public void addInfo(String fName, String Lname, SQLiteDatabase db){

    ContentValues contentValues = new ContentValues();
    contentValues.put(TableData.TableInfo.USER_FIRSTNAME, fName);
    contentValues.put(TableData.TableInfo.USER_LASTNAME,Lname);
    db.insert(TableData.TableInfo.USER_TABLE, null, contentValues);

    Log.e("DB OPERATIONS", "Data has been inserted");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

And finally initializing the functionality of my submit button that is in the activity_main.xml in the MainActivity class

    public class MainActivity extends AppCompatActivity {

EditText fName, lName;
MyDbHelper myDbHelper;
Context context;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fName = (EditText) findViewById(R.id.fname);
    lName = (EditText) findViewById(R.id.lname);

}

public void onClick(View v){
    String firstname = fName.getText().toString();
    String lastname = lName.getText().toString();

    myDbHelper = new MyDbHelper(context);
    sqLiteDatabase = myDbHelper.getWritableDatabase();
    myDbHelper.addInfo(firstname, lastname, sqLiteDatabase);

    Toast.makeText(getBaseContext(), "DATA HAS BEEN INSERTED", Toast.LENGTH_LONG).show();
    myDbHelper.close();
}

}

Sticky 256
  • 11
  • 1
  • Possible duplicate of [android java.langNullPointerException null object reference](http://stackoverflow.com/questions/29524453/android-java-langnullpointerexception-null-object-reference) – Al Lelopath Apr 13 '17 at 14:32

1 Answers1

0

You're not initialising context in your activity, therefore you're passing a null context into your MyDbHelper. As you're calling from an Activity (which extends Context), try this instead:

myDbHelper = new MyDbHelper(this);
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • Thanks that threw a lot of light Context context = this; – Sticky 256 Apr 13 '17 at 14:52
  • @Sticky256 You don't even need to use `Context context;` at all in this example. Since `AppCompatActivity` extends `Context`, just use `this` whenever you need a `Context`. – Michael Dodd Apr 13 '17 at 14:56