为啥布尔 DBHelper.updateStudents(java.lang.String,java.lang.String,java.lang.String,java.lang.String, ja
Posted
技术标签:
【中文标题】为啥布尔 DBHelper.updateStudents(java.lang.String,java.lang.String,java.lang.String,java.lang.String, java.lang.String) on nullobjectreference【英文标题】:Why is boolean DBHelper.updateStudents(java.lang.String,java.lang.String,java.lang.String,java.lang.String, java.lang.String)on nullobjectreference为什么布尔 DBHelper.updateStudents(java.lang.String,java.lang.String,java.lang.String,java.lang.String, java.lang.String) on nullobjectreference 【发布时间】:2021-09-01 03:42:18 【问题描述】:我有一个活动,它从 SQLite 数据库获取用户数据并显示在编辑文本中,允许用户更改他的信息。进行更改时,将调用 Db.update 函数。我收到此错误。
btnSave.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String user = fullname.getEditText().getText().toString();
String email_id = email.getEditText().getText().toString();
String phone = number.getEditText().getText().toString();
String countrycode = ccp.getSelectedCountryCode();
String pass = password.getEditText().getText().toString();
String ve_pass = verifypassword.getEditText().getText().toString();
Toast.makeText(user_info.this, user+email_id+phone, Toast.LENGTH_SHORT).show();
if (user.equals("") || pass.equals("") || ve_pass.equals("") || email_id.equals("") || (phone.equals("")))
Toast.makeText(user_info.this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
else
if (pass.equals(ve_pass))
Boolean update = DB.updateStudents(user, email_id, countrycode, phone, pass);
if ( update== true)
Toast.makeText(user_info.this, "updated successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), add_device.class);
startActivity(intent);
这是日志猫
2021-06-16 14:55:46.380 18829-18829/nanodevlab.screens E/androidRuntime: FATAL EXCEPTION: main
Process: nanodevlab.screens, PID: 18829
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean nanodevlab.screens.DBHelper.updateStudents(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at nanodevlab.screens.user_info$1.onClick(user_info.java:81)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Strings中的值不为空,我已经单独检查过了。
这是我在 DBHELPER 类中的更新函数
public boolean updateStudents(String username, String email, String countrycode, String number, String password)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put("username", username);
contentValues.put("email", email);
contentValues.put("countrycode", countrycode);
contentValues.put("number", number);
contentValues.put("password", password);
db.update(TABLE_USER, contentValues, email + " = ? " ,
new String[]String.valueOf(email));
return true;
【问题讨论】:
【参考方案1】:这意味着您的DB
变量未初始化。在调用某些调用之前,您应该初始化 DB
变量:
DB = new DbHelper(); // or something like this
Boolean update = DB.updateStudents(user, email_id, countrycode, phone, pass);
【讨论】:
在按钮事件监听之前初始化。还是不行。以上是关于为啥布尔 DBHelper.updateStudents(java.lang.String,java.lang.String,java.lang.String,java.lang.String, ja的主要内容,如果未能解决你的问题,请参考以下文章