我希望这是一个非常基本的东西,我只是没有看到 - 但我想知道为什么以下语句调用更新客户端的方法,在update方法的返回行引起NullPointerException。
如果有帮助知道,我有插入并选择工作正常并通过调试我知道update语句在执行之前存储了所有三个变量但从未执行。
方法调用
public void onSaveButtonClick(View v) {
DBAdapter db = new DBAdapter(this);
// Create and set fields
EditText nameField = (EditText) findViewById(R.id.EditText_Name);
EditText emailField = (EditText) findViewById(R.id.EditText_Email);
name = nameField.toString();
email = emailField.toString();
db.updateClient(_id, "Test Name", "Test Email");
Toast.makeText(PTViewClient.this,"TODO: Save Button Functionality", Toast.LENGTH_SHORT).show();
}
更新客户端方法抛出NullPointerException
public boolean updateClient(long clientID, String name, String email)
{
ContentValues args = new ContentValues();
args.put(KEY_CLIENT_NAME, name);
args.put(KEY_CLIENT_EMAIL, email);
return db.update(DATABASE_CLIENTS, args, KEY_CLIENT_ID + "=" + clientID, null) > 0;
}
堆栈跟踪
05-15 12:47:02.978: E/AndroidRuntime(22110): FATAL EXCEPTION: main
05-15 12:47:02.978: E/AndroidRuntime(22110): java.lang.IllegalStateException: Could not execute method of the activity
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.view.View$1.onClick(View.java:3599)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.view.View.performClick(View.java:4204)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.view.View$PerformClick.run(View.java:17355)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.os.Handler.handleCallback(Handler.java:725)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.os.Looper.loop(Looper.java:137)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-15 12:47:02.978: E/AndroidRuntime(22110): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:47:02.978: E/AndroidRuntime(22110): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 12:47:02.978: E/AndroidRuntime(22110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-15 12:47:02.978: E/AndroidRuntime(22110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-15 12:47:02.978: E/AndroidRuntime(22110): at dalvik.system.NativeStart.main(Native Method)
05-15 12:47:02.978: E/AndroidRuntime(22110): Caused by: java.lang.reflect.InvocationTargetException
05-15 12:47:02.978: E/AndroidRuntime(22110): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:47:02.978: E/AndroidRuntime(22110): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 12:47:02.978: E/AndroidRuntime(22110): at android.view.View$1.onClick(View.java:3594)
05-15 12:47:02.978: E/AndroidRuntime(22110): ... 11 more
05-15 12:47:02.978: E/AndroidRuntime(22110): Caused by: java.lang.NullPointerException
05-15 12:47:02.978: E/AndroidRuntime(22110): at com.personaltrainer.DBAdapter.updateClient(DBAdapter.java:173)
05-15 12:47:02.978: E/AndroidRuntime(22110): at com.personaltrainer.PTViewClient.onSaveButtonClick(PTViewClient.java:124)
05-15 12:47:02.978: E/AndroidRuntime(22110): ... 14 more
答案 0 :(得分:0)
在调用
之前,只需检查db和_id是否为空指针db.updateClient(_id, "Test Name", "Test Email");
答案 1 :(得分:0)
使用这种方式
return db.update(DATABASE_CLIENTS, KEY_CLIENT_ID + "=" + clientID, null) > 0;
答案 2 :(得分:0)
您需要打开数据库
DBAdapter db = new DBAdapter(this);
db.open();