can anyone tell wats wrong in this code... I put code bellow.
mySQLiteAdapter = new SQLiteAdapter(this);
cursor = mySQLiteAdapter.displayCategory();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CATEGORY };
int[] to = new int[] { android.R.id.text1 };
mCategory = (Spinner) findViewById(R.id.choose_category);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCategory.setAdapter(adapter);
MyAdapter class having following code
public Cursor displayCategory() {
String[] columns = new String[]{KEY_ID, KEY_CATEGORY};
Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, col开发者_JS百科umns,
null, null, null, null, null);
return cursor;
Null pointer Exception. How to avoid it. This is the error on log cat
09-09 15:49:16.535: ERROR/AndroidRuntime(6526): Uncaught handler: thread main exiting due to uncaught exception
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mpt.receiptor/com.android.mpt.receiptor.SearchReceipt}: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.os.Looper.loop(Looper.java:123)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at java.lang.reflect.Method.invoke(Method.java:521)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at dalvik.system.NativeStart.main(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): Caused by: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.mpt.receiptor.SQLiteAdapter.displayCategory(SQLiteAdapter.java:135)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.mpt.receiptor.SearchReceipt.onCreate(SearchReceipt.java:96)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
Pls someone help me.
Thanks in advance
It looks like in this line:
Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns,
null, null, null, null, null);
that sqLiteDatabase
is null - make sure it is initialised in your onCreate
method.
why cant you put a breakpoint in the displayCategory() method? null pointer exceptions are quite easy to find.As Cjk has pointed out, sqlitedatabase is unitialised. Debugging will help you find and solve issues before you lose much of your time..
精彩评论