我想获取最新输入的第1列数据,因此我实现了以下代码,但它失败了。
我在本网站上研究过很多关于“SELECT MAX(_id)FROM”的问题和答案,但似乎仍无法取得成功(Logcat如下图所示):
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " ( " +
" _id int, " + ...
public long getMaxID()
{
int id = 0;
final String MY_QUERY = "SELECT MAX(_id) AS _id FROM " + TABLE_NAME;
Cursor mCursor = database.rawQuery(MY_QUERY, null); //LINE 80
if (mCursor.getCount() > 0)
{
mCursor.moveToFirst();
id = mCursor.getInt(mCursor.getColumnIndex("_id"));
}
return id;
DataBaseHelper databaseConnector = new DataBaseHelper(Profile.this);
long ID = databaseConnector.getMaxID();
try
{
new LoadDataTask().execute(ID);
}
catch (SQLiteException e)
{
System.err.println("Exception Message: " + e.getMessage());
}
}
08-30 00:22:00.105: E/AndroidRuntime(8658): FATAL EXCEPTION: main
08-30 00:22:00.105: E/AndroidRuntime(8658): java.lang.RuntimeException: Unable to resume activity {com.abc.abc/com.abc.abc.Profile}: java.lang.NullPointerException
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.os.Looper.loop(Looper.java:137)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-30 00:22:00.105: E/AndroidRuntime(8658): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 00:22:00.105: E/AndroidRuntime(8658): at java.lang.reflect.Method.invoke(Method.java:511)
08-30 00:22:00.105: E/AndroidRuntime(8658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-30 00:22:00.105: E/AndroidRuntime(8658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-30 00:22:00.105: E/AndroidRuntime(8658): at dalvik.system.NativeStart.main(Native Method)
08-30 00:22:00.105: E/AndroidRuntime(8658): Caused by: java.lang.NullPointerException
08-30 00:22:00.105: E/AndroidRuntime(8658): at com.abc.abc.Abc_Measures_DataBaseHelper.getMaxID(Abc_Measures_DataBaseHelper.java:80)
08-30 00:22:00.105: E/AndroidRuntime(8658): at com.abc.abc.Profile.onResume(Profile.java:735)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.Activity.performResume(Activity.java:5280)
08-30 00:22:00.105: E/AndroidRuntime(8658): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
08-30 00:22:00.105: E/AndroidRuntime(8658): ... 12 more
答案 0 :(得分:1)
Cursor mCursor = database.rawQuery(MY_QUERY, null); //LINE 80
database
是null
(Caused by: java.lang.NullPointerException
)。在使用之前,您需要初始化database
数据成员。