Android:Get single row from SQlLite, Null Pointer exception


Ask by : Calgar99 November 04, 2012 02:36

I am trying to receive a row id from from a main activity and from there, display the content of that row. However I am getting a null pointer exception. I'm unsure of how to implement this properly.

The exception is related to line 22 MODULEDATABASE.openToRead(); I am an unsure if I need to open to read as I have done this in the main activity. If I comment out this line the next error refers to line 23 cursor = MODULEDATABASE.getRow(rowId); Any suggestions would be much appreciated

LogCat error from **MODULEDATABASE.openToRead();

    10-23 09:12:52.572: E/AndroidRuntime(19778): Uncaught handler: thread main exiting due to uncaught exception
10-23 09:12:52.582: E/AndroidRuntime(19778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.ViewCourse}: java.lang.NullPointerException
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.os.Looper.loop(Looper.java:123)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.main(ActivityThread.java:4363)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at java.lang.reflect.Method.invoke(Method.java:521)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at dalvik.system.NativeStart.main(Native Method)
10-23 09:12:52.582: E/AndroidRuntime(19778): Caused by: java.lang.NullPointerException
10-23 09:12:52.582: E/AndroidRuntime(19778):    at com.example.mycoursetimetable.ViewCourse.onCreate(ViewCourse.java:22)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 09:12:52.582: E/AndroidRuntime(19778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-23 09:12:52.582: E/AndroidRuntime(19778):    ... 11 more
10-23 09:12:52.603: I/dalvikvm(19778): threadid=7: reacting to signal 3
10-23 09:12:52.603: E/dalvikvm(19778): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-23 09:12:59.232: I/Process(19778): Sending signal. PID: 19778 SIG: 9

LogCat cursor = MODULEDATABASE.getRow(rowId);

10-23 09:16:42.923: E/AndroidRuntime(20316): Uncaught handler: thread main exiting due to uncaught exception
10-23 09:16:42.933: E/AndroidRuntime(20316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.ViewCourse}: java.lang.NullPointerException
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.os.Looper.loop(Looper.java:123)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.main(ActivityThread.java:4363)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at java.lang.reflect.Method.invoke(Method.java:521)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at dalvik.system.NativeStart.main(Native Method)
10-23 09:16:42.933: E/AndroidRuntime(20316): Caused by: java.lang.NullPointerException
10-23 09:16:42.933: E/AndroidRuntime(20316):    at com.example.mycoursetimetable.ViewCourse.onCreate(ViewCourse.java:23)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 09:16:42.933: E/AndroidRuntime(20316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-23 09:16:42.933: E/AndroidRuntime(20316):    ... 11 more
10-23 09:16:42.953: I/dalvikvm(20316): threadid=7: reacting to signal 3
10-23 09:16:42.953: E/dalvikvm(20316): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-23 09:16:44.633: I/Process(20316): Sending signal. PID: 20316 SIG: 9

View Course activity

public class ViewCourse extends Activity {
Cursor cursor;
database MODULEDATABASE;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_course);

        Intent intent = getIntent();
        String rowId = intent.getStringExtra(MyCourses.TEST);
        //MODULEDATABASE.openToRead();
        cursor = MODULEDATABASE.getRow(rowId);

        TextView text_modulecode = (TextView)findViewById(R.id.viewModuleCode);
        TextView text_modulename = (TextView)findViewById(R.id.viewModuleName);

        text_modulecode.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULECODE)));
        text_modulename.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULENAME)));

    }

Database Class

public  database openToRead()throws SQLException{
        myHelper = new DbHelper(myContext);
        moduleDatabase = myHelper.getReadableDatabase();
        return this;
}

public Cursor getRow(String rowId)
    {
        String[] columns = new String [] {KEY_ROWID,KEY_MODULECODE, KEY_MODULENAME};
        Cursor c = moduleDatabase.query(DATABASE_TABLE, columns,KEY_ROWID+ "="+rowId , null, null, null, null);
        if(c!=null){
            c.moveToFirst();
        }
        return c;
    }
View original question

Answer by : JamesNovember 04, 2012 02:30

I'd check what myContext is.

myHelper = new DbHelper(myContext);

You have created MODULEDATABASE as a global which is correct however you need to initialize the object in your onCreate method of the Course activity. Ensure that the initialization of the database object accepts the context from your Course activity. That is, have a constructor that has a parameter for the course activity to pass its context over.

In your Course Activity you should be able to do the following...

database MODULEDATABASE;
public void onCreate(...) {
    MODULEDATABASE= new database(getApplicationContext()); 
} 

EDIT: The solution above gives the correct context when creating the object. Using ADR's solution and instead of ViewCourse.this, use getApplicationContext() should give you another way of doing this.

View original answer