在android中获取数据库信息时出现NullPointerException

时间:2013-04-26 18:50:14

标签: java android database android-cursoradapter

我想用这种方法获取数据库记录并存储在一个对象中:

    SQLiteDatabase db = openOrCreateDatabase("lingodb",MODE_PRIVATE, null);        
    Cursor cur;
    cur = db.rawQuery("Select * from lingo where stage='1'", null);

    cur.moveToFirst();
    int n=0;
    while (!cur.isAfterLast()) {
        words_array[n].word = cur.getString(cur.getColumnIndex("word"));
        words_array[n].pack = cur.getInt(cur.getColumnIndex("stage"));
        words_array[n].id = cur.getInt(cur.getColumnIndex("pack_id"));
        n=n+1;
        cur.moveToNext();
    }
    cur.close();
    db.close();

我在db中的信息是有效的,但是这段代码提供了这个错误:

04-26 23:15:52.090: E/AndroidRuntime(22002): FATAL EXCEPTION: main
04-26 23:15:52.090: E/AndroidRuntime(22002): java.lang.RuntimeException: Unable to   start activity ComponentInfo{com.english.game/com.english.game.MainActivity}: java.lang.NullPointerException
    04-26 23:15:52.090: E/AndroidRuntime(22002):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.os.Handler.dispatchMessage(Handler.java:99)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.os.Looper.loop(Looper.java:137)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.ActivityThread.main(ActivityThread.java:5041)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at java.lang.reflect.Method.invokeNative(Native Method)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at java.lang.reflect.Method.invoke(Method.java:511)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at dalvik.system.NativeStart.main(Native Method)
  04-26 23:15:52.090: E/AndroidRuntime(22002): Caused by: java.lang.NullPointerException
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at com.english.game.MainActivity.onCreate(MainActivity.java:75)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.Activity.performCreate(Activity.java:5104)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
   04-26 23:15:52.090: E/AndroidRuntime(22002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

为什么会这样?

1 个答案:

答案 0 :(得分:2)

您是否为words_array分配空间,不是吗?

在您提供的代码段中,words_arraycur都可以为空。请务必为要填充的阵列分配空间。由于stacktrace引用了行

words_array[n].word = cur.getString(cur.getColumnIndex("word"))

cur不能为空,只有words_array可以为空