2013-04-26 26 views
0

我想在一个对象来获取数据库记录机智htis方法和STOR数据库信息时:的NullPointerException获取Android中

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

什么是线75 IH MainActivity类别? – rciovati 2013-04-26 18:52:16

+0

@rciovati:this line:words_array [n] .word = cur.getString(cur.getColumnIndex(“word”)); – Fcoder 2013-04-26 18:54:29

回答

2

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

在您提供的代码段中,words_arraycur可能为空。请确保为正在填充的阵列分配空间。由于堆栈跟踪referes到线

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

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

+0

是的,我做到了。我的问题是我的问题是从数据库中获取信息 – Fcoder 2013-04-26 18:55:44

+0

哇,你做到了。实际上,我忘记了为words_array分配空间......谢谢我的朋友 – Fcoder 2013-04-26 18:58:08

+0

不客气。 – Blackbelt 2013-04-26 18:59:02

相关问题