0

i want to fetch database records wit htis method and stor in an object:

    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();

my information in db is valid but this code provide this error:

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)

why this happen?

4

1 回答 1

2

你有分配空间words_array,不是吗?

在您提供两者的片段中,words_array并且cur可能为空。请确保为要填充的数组分配空间。由于堆栈跟踪引用了该行

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

并且cur不能为空,只能words_array为空

于 2013-04-26T18:52:35.057 回答