我试图使用 HashMap 从数据库中读取数据,但每次我在模拟器中运行该程序时都会受到攻击。我尝试使用带有** BasicNameValuePair *的Arraylist来检索数据,但这也没有用完。 这是我读取数据的代码。
DatabaseHandler databaseHandler = new DatabaseHandler(
getApplicationContext());
HashMap<String, String> user = new HashMap<String, String>();
user = databaseHandler.userDetails();
email = user.get("email").toString();
password = user.get("password").toString();
这是我的userDetails()方法
public HashMap<String, String> userDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String query = "SELECT * FROM login";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
// move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("email", cursor.getString(1));
user.put("password", cursor.getString(2));
}
cursor.close();
db.close();
return user;
}
logcat显示错误
07-16 17:13:23.272: E/AndroidRuntime(1396): at com.ved_main.mainpage.VerifyMainActivity.onCreate(VerifyMainActivity.java:56)
07-16 17:54:10.332: E/AndroidRuntime(1501): FATAL EXCEPTION: main
07-16 17:54:10.332: E/AndroidRuntime(1501): Process: com.ved_main.mainpage, PID: 1501
07-16 17:54:10.332: E/AndroidRuntime(1501): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ved_main.mainpage/com.ved_main.mainpage.VerifyMainActivity}: java.lang.NullPointerException
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.os.Looper.loop(Looper.java:136)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 17:54:10.332: E/AndroidRuntime(1501): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 17:54:10.332: E/AndroidRuntime(1501): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 17:54:10.332: E/AndroidRuntime(1501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 17:54:10.332: E/AndroidRuntime(1501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 17:54:10.332: E/AndroidRuntime(1501): at dalvik.system.NativeStart.main(Native Method)
07-16 17:54:10.332: E/AndroidRuntime(1501): Caused by: java.lang.NullPointerException
07-16 17:54:10.332: E/AndroidRuntime(1501): at com.ved_main.mainpage.VerifyMainActivity.onCreate(VerifyMainActivity.java:54)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.Activity.performCreate(Activity.java:5231)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 17:54:10.332: E/AndroidRuntime(1501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 17:54:10.332: E/AndroidRuntime(1501): ... 11 more
答案 0 :(得分:0)
该表没有任何行,因此email
对象中没有user
条目。