-2

I'm a newbie in android world, when I try to get data from the database, I meet this error, bellow is my database access code

public static boolean Checkduplicate(String activity_name, String location, String date) {
    SQLiteDatabase dtb = ActivityHandler.db;
    String Query = "Select * from  Activity  where activity_name = " + activity_name + "and location =" + location + "and _date =" + date;
    Cursor cursor = dtb.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return true;
    }
    cursor.close();
    return false;
}

Here is the error

FATAL EXCEPTION: main
              Process: com.example.vinhg.comp1661_nguyengiavinh, PID: 31092
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
                  at com.example.vinhg.comp1661_nguyengiavinh.ActivityHandler.Checkduplicate(ActivityHandler.java:32)
                  at com.example.vinhg.comp1661_nguyengiavinh.MainActivity.addData(MainActivity.java:41)
                  at com.example.vinhg.comp1661_nguyengiavinh.MainActivity$1.onClick(MainActivity.java:32)
                  at android.view.View.performClick(View.java:5340)
                  at android.view.View$PerformClick.run(View.java:21610)
                  at android.os.Handler.handleCallback(Handler.java:815)
                  at android.os.Handler.dispatchMessage(Handler.java:104)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5763)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)

Here is my full ActivityHandler class

public class ActivityHandler {
private static SQLiteDatabase db;
public  ActivityHandler(Context context){
    DatabaseHandler dbDatabaseHandler = new DatabaseHandler(context);
    this.db = dbDatabaseHandler.getWritableDatabase();
}

@Override
protected void finalize() throws Throwable {
    try{
        db.close();
    }catch (Exception ex){

    }
    super.finalize();
}

public static boolean Checkduplicate(String activity_name, String location, String date) {
    SQLiteDatabase dtb = ActivityHandler.db;
    String Query = "Select * from  Activity  where activity_name = " + activity_name + "and location =" + location + "and _date =" + date;
    Cursor cursor = dtb.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return true;
    }
    cursor.close();
    return false;
}
Vinh Nguyễn
  • 61
  • 2
  • 9

2 Answers2

0

You need to understand what does static mean and how Java Garbage Collector works. You are assigning value to static property in object constructor. It means 2 things:
1) You need to create an object of ActivityHandler first. It will initialize property db with value.
2) You will need to keep reference to this object somewhere. If you will not keep the reference, object will be garbage collected which will call finalize() and close db.

I'd suggest following:
1) Remove keyword static, and pass an object of ActivityHandler to the method
2) Read carefully some basic Java books like Core Java, Effective Java etc.

noktigula
  • 416
  • 6
  • 13
0

I think you should remove static and write this =>

public class ActivityHandler {
private SQLiteDatabase db;
public ActivityHandler(Context context){
    DatabaseHandler dbDatabaseHandler = new DatabaseHandler(context);
    this.db = dbDatabaseHandler.getWritableDatabase();
}

@Override
protected void finalize() throws Throwable {
    try{
        db.close();
    }catch (Exception ex){

    }
    super.finalize();
}

public boolean Checkduplicate(String activity_name, String location, String date) {
    SQLiteDatabase dtb = ActivityHandler.db;
    String Query = "Select * from  Activity  where activity_name = " + activity_name + "and location =" + location + "and _date =" + date;
    Cursor cursor = dtb.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return true;
    }
    cursor.close();
    return false;
}

And then in another class you create a new instance and call you method.

ActivityHandler activityHandler = new ActivityHandler(context);
boolean isDuplicate = activityHandler.Checkduplicate(activity_name,location, date);
olivejp
  • 680
  • 6
  • 14