0

So, this is my database helper class

public class DBHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME =  "EventManager.db";
    public static final String TABLE_NAME =  "EventManager";
    public static final String Col_1 =  "NAME";
    public static final String Col_2 =  "DATE";
    public static final String Col_3 =  "TIME";
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (NAME TEXT, DATE TEXT, TIME TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public boolean insertData(String name, String date, String time){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Col_1, name);
        contentValues.put(Col_2, date);
        contentValues.put(Col_3, time);
        db.insert(TABLE_NAME, null, contentValues);
        return true;
    }
}

I am trying to call insertData() from another class like:

public void addData(View view){
    if(dbHelper.insertData(editTitle.getText().toString(), date.getText().toString(), 
                    time.getText().toString())) 
        Toast.makeText(AddEventActivity.this, "Event saved", Toast.LENGTH_LONG).show();                
    else 
        Toast.makeText(AddEventActivity.this, "Event not saved", Toast.LENGTH_LONG).show();
}

addData() is called when a button is clicked.

So, whenever I attempt to save data using the code, my app is crashing. The issue is with database connection, I have checked all the editTexts, button calling, everything is fine. It's crashing only when database is being involved and when I tried without DB and a simple toast displaying editText values on a button click, it's working fine. Please help.

Logcat :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.yogith.exampleevent.DBHelper.insertData(java.lang.String, java.lang.String, java.lang.String)' on a null object reference
                                                                               at com.example.yogith.exampleevent.AddEventActivity.addData(AddEventActivity.java:87)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5201) 

error

07-02 12:56:37.235 9949-9949/com.example.yogith.exampleevent W/System: ClassLoader referenced unknown path: /data/app/com.example.yogith.exampleevent-1/lib/arm

Solved: The DBHelper object, 'dBHelper' that is used call addData() wasn't declared properly. Thanks

0 Answers0