DBAdapter类函数:
public void getValues(){
Cursor mCursor = db.query(DBAdapter.DATABASE_TABLE2, new String[] {
EDGE_ID,
SOURCE_NODE_ID,
DESTINATION_NODE_ID,
VALUE1}, null, null, null, null, null);
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
mArrayList.add(mCursor.getString(mCursor.getColumnIndex(DBAdapter.SOURCE_NODE_ID)));
mCursor.moveToNext();
}
for(String d1:mArrayList) {
System.out.println(d1);
}
}
以下是主要活动类:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final DBAdapter db = new DBAdapter(this);
db.open();
db.getValues();
db.close();
}
根据上面的代码,我应该得到一个数组列表,其中包含我的数据库表的值
SOURCE_NODE_ID
属性。 我想在LogCat中打印这个arraylist。但我得到以下错误:
05-30 14:51:39.435: E/AndroidRuntime(4954): FATAL EXCEPTION: main
05-30 14:51:39.435: E/AndroidRuntime(4954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navigationsystem/com.navigationsystem.NavigationSystemActivity}: java.lang.NullPointerException
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.os.Looper.loop(Looper.java:137)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 14:51:39.435: E/AndroidRuntime(4954): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 14:51:39.435: E/AndroidRuntime(4954): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 14:51:39.435: E/AndroidRuntime(4954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 14:51:39.435: E/AndroidRuntime(4954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 14:51:39.435: E/AndroidRuntime(4954): at dalvik.system.NativeStart.main(Native Method)
05-30 14:51:39.435: E/AndroidRuntime(4954): Caused by: java.lang.NullPointerException
05-30 14:51:39.435: E/AndroidRuntime(4954): at com.navigationsystem.DBAdapter.getValues(DBAdapter.java:117)
05-30 14:51:39.435: E/AndroidRuntime(4954): at com.navigationsystem.NavigationSystemActivity.onCreate(NavigationSystemActivity.java:53)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.Activity.performCreate(Activity.java:4465)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-30 14:51:39.435: E/AndroidRuntime(4954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-30 14:51:39.435: E/AndroidRuntime(4954): ... 11 more
此代码有什么问题。
感谢。
答案 0 :(得分:0)
05-30 14:51:39.435: E/AndroidRuntime(4954): Caused by: java.lang.NullPointerException
05-30 14:51:39.435: E/AndroidRuntime(4954): at com.navigationsystem.DBAdapter.getValues(DBAdapter.java:117)
您的代码中有NullPointer
。
我认为mArrayList
方法中的null
为getValue()
。你在哪里创建那个List?
db
也可能null
DBAdapter
。{{1}}你试过调试吗?你输出了变量吗?