beginner beginner - 1 year ago 169
Android Question

Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query on null object reference

I have an activity in which i am trying to get the list of data based on some condition . This selectsome() function is in the SQLiteOpenHelper subclass.

MyDBHandler dbhelper = new MyDBHandler(this);
Cursor cursor;
cursor = dbhelper.selectSome(value);


In class MyDBHelper.java

public Cursor selectSome(String arg)

{

String columns[]=new String[]{"_id","_hotelname","_city","_imageSrc","_rent","_address"};
String selarg[]={arg};
Cursor c=db.query(TABLE_NAME,columns,"city = ?",selarg, null,null,null);
return c;
}


I am getting this error please help.

09-01 18:08:00.394 E/AndroidRuntime( 6246): FATAL EXCEPTION: main

09-01 18:08:00.394 E/AndroidRuntime( 6246): Process: com.example.hari.testapp, PID: 6246

09-01 18:08:00.394 E/AndroidRuntime( 6246): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hari.testapp/com.example.hari.testapp.HotelList}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.app.ActivityThread.access$800(ActivityThread.java:151)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.os.Handler.dispatchMessage(Handler.java:102)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.os.Looper.loop(Looper.java:135)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at android.app.ActivityThread.main(ActivityThread.java:5257)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at java.lang.reflect.Method.invoke(Native Method)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at java.lang.reflect.Method.invoke(Method.java:372)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

09-01 18:08:00.394 E/AndroidRuntime( 6246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

09-01 18:08:00.394 E/AndroidRuntime( 6246): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference

09-01 18:08:00.394 E/AndroidRuntime( 6246): at com.example.hari.testapp.databaseFiles.MyDBHandler.selectSome(MyDBHandler.java:113)

Answer Source

Your variable db in the helper is not initialized. Call getWritableDatabase() or getReadableDatabase() to obtain a SQLiteDatabase reference you can then assign to that variable before the query:

db = getWritableDatabase();
Cursor c = db.query(...);