NullPointerException when insertData() called in android

I am currently working on android stenography project, but at registration time the error was occur

"java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.devd.stangnograpyonimage.DBHelper.insertData(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference"

DataBaseHelper.java

public static final String DATABASE_NAME = "STAGNODB";
public static final String TABLE_NAME = "Satgno_Table";
//public static final String COLUUMN_ID  = "ID";
public static final String COLUUMN_USERNAME  = "UserName";
public static final String COLUUMN_PASSWORD = "Password";
public static final String COLUUMN_NAME = "Name";
public static final String COLUUMN_EMAIL = "Email";


public DBHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + COLUUMN_USERNAME + " VARCHAR(50),"
                + COLUUMN_PASSWORD + " VARCHAR(50),"
                + COLUUMN_NAME + " VARCHAR(50),"
                + COLUUMN_EMAIL + " VARCHAR(50));", null);
    }catch (Exception e){
        Log.e("SQL Table Creation ", "Table Not Created");
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}

public  boolean insertData(String userName, String password, String name, String email){
    SQLiteDatabase mydb = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUUMN_USERNAME, userName);
    cv.put(COLUUMN_PASSWORD, password);
    cv.put(COLUUMN_NAME, name);
    cv.put(COLUUMN_EMAIL, email);
    long result = mydb.insert(TABLE_NAME, null, cv);

    if(result == -1){
        return  false;
    }
    else {
        return  true;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    DBHelper myDB;
    EditText etUserName, etPassword, etName, etEmail;
    Button btnRegister, btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUserName = (EditText) findViewById(R.id.etUserName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etName = (EditText) findViewById(R.id.etName);
        etEmail = (EditText) findViewById(R.id.etEmail);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        register();
    }
    public void register() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted = myDB.insertData(etUserName.getText().toString(), etPassword.getText().toString(), etName.getText().toString(), etEmail.getText().toString());
                if(isInserted==true){
                    Toast.makeText(MainActivity.this,"Data Inserted", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"Data not Inserted", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

2 answers

  • answered 2018-04-17 04:17 EJK

    In your activity class, you have not initialized the variable DBHelper myDB. Thus when your activity calls

    myDB.insertData(etUserName.getText().toString(), ...
    

    You get a NullPointerException because myDB is null. To fix this, create the object in the onCreate method of your activity

    myDB = new DBHelper(this);
    

  • answered 2018-04-17 04:17 MikeT

    You are declaring but not instantiating myDB so it is null.

    i.e.

        DBHelper myDB; 
    

    Just declares myDB so it is null at this point as nothing has been assigned to the variable.

    You need to add something like :-

        myDB = new DBHelper(this);
    

    In the onCreate method of the MainActivity.

    e.g. :-

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUserName = (EditText) findViewById(R.id.etUserName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etName = (EditText) findViewById(R.id.etName);
        etEmail = (EditText) findViewById(R.id.etEmail);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        myDB = new DBHelper(this); //<<<< ADDED
        register();
    }