Search code examples
javaandroidlistviewandroid-sqlite

getReadableDatabase()' on a null object reference


I'm new, I am doing some debugging. I am having some difficulty that I am having some difficulty conveying.

I am trying to read from a SQLite database I have created in another activity.

My current problem appears to be:

SQLiteDatabase db = myOpenHelper.getReadableDatabase();

which should point to my database helper class.

public class SecondActivity extends ListActivity {

    CustomOpenHelper myOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        displayDataInTable();

    }

        void displayDataInTable() {
        List<String> values = queryTable();

        if (values != null) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(SecondActivity.this, android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);


        }

    }

    List<String> queryTable() {
        List<String> player = new ArrayList<String>();

        SQLiteDatabase db = myOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(true, CustomOpenHelper.TABLE_NAME, new String[]{"_id, name, score"}, null, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int score = cursor.getInt(cursor.getColumnIndex("score"));
            player.add(id + " --> the player " + name + " has got a score of " + score + "s");
        }

        return player;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The LogCat error (extract):

09-28 20:41:57.039    1271-1285/system_process W/ActivityManager﹕ Launch timeout has expired, giving up wake lock!
09-28 20:42:23.468    3642-3642/com.example.eagle.sqlite2activitytest D/AndroidRuntime﹕ Shutting down VM
09-28 20:42:23.486    3642-3642/com.example.eagle.sqlite2activitytest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.eagle.sqlite2activitytest, PID: 3642
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eagle.sqlite2activitytest/com.example.eagle.sqlite2activitytest.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.eagle.sqlite2activitytest.CustomOpenHelper.getReadableDatabase()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.eagle.sqlite2activitytest.CustomOpenHelper.getReadableDatabase()' on a null object reference
            at com.example.eagle.sqlite2activitytest.SecondActivity.queryTable(SecondActivity.java:43)
            at com.example.eagle.sqlite2activitytest.SecondActivity.displayDataInTable(SecondActivity.java:29)
            at com.example.eagle.sqlite2activitytest.SecondActivity.onCreate(SecondActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-28 20:42:23.493    1271-1721/system_process W/ActivityManager﹕ Force finishing activity com.example.eagle.sqlite2activitytest/.SecondActivity

What do you think?


Solution

  • try this you can create proper object like this.

    CustomOpenHelper myOpenHelper = new CustomOpenHelper(this);
    

    and then write following statement,

    SQLiteDatabase db = myOpenHelper.getReadableDatabase();