非常奇怪的Android NullPointerException

时间:2014-04-21 12:33:23

标签: java android listview arraylist nullpointerexception

我一直在努力使用从我的sqlite数据库中检索到的数据填充ListView, 我的数据库文件DBMainAdapter中的代码完美运行(代码片段以节省空间)我知道这一点,因为当我在一个单独的活动中调用我的getAll()时,我使用(不包括)我得到我的{ {1}}我可以完全正确地使用它打印出里面的内容但是我注意到,当我尝试在我的ArrayList内做同样的事情时,我不断得到ProfileListActivity抛出。我做错了什么?!

错误日志:

NullPointerException

com.test.profilekeeper.LoadedData。(LoadedData.java:17):ArrayList listdata = dbhelper.getAll(); //构造

com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)的位置:LoadedData mydata = new LoadedData();

04-21 06:42:31.669: W/dalvikvm(6107): threadid=1: thread exiting with uncaught exception (group=0xb3a8db90)
04-21 06:42:31.749: E/AndroidRuntime(6107): FATAL EXCEPTION: main
04-21 06:42:31.749: E/AndroidRuntime(6107): Process: com.test.profilekeeper, PID: 6107
04-21 06:42:31.749: E/AndroidRuntime(6107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.profilekeeper/com.test.profilekeeper.ProfileListActivity}: java.lang.NullPointerException
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.os.Looper.loop(Looper.java:137)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at java.lang.reflect.Method.invoke(Method.java:515)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at dalvik.system.NativeStart.main(Native Method)
04-21 06:42:31.749: E/AndroidRuntime(6107): Caused by: java.lang.NullPointerException
04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.test.profilekeeper.LoadedData.<init>(LoadedData.java:17)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.Activity.performCreate(Activity.java:5243)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-21 06:42:31.749: E/AndroidRuntime(6107):     ... 11 more

1 个答案:

答案 0 :(得分:5)

您忘记在LoadedData类中初始化dbhelper对象。在类中创建一个公共构造函数,如下所示,

public DBMainAdapter(Context context)
{
    dbhelper = new DBMain(context);
} 

同样在DBMainAdapter类的getAll()方法中,在while循环之前写下面的语句,

curs.moveToFirst();