gpt4 book ai didi

java - DbHelper 类上下文中出现奇怪的空指针异常,其中的代码

转载 作者:行者123 更新时间:2023-12-01 04:31:45 25 4
gpt4 key购买 nike

我在这段代码上得到了一个空指针...所以迷失了。

我有一个名为 DbAdapter 的数据库辅助类。

public class DbAdapter {

private final Context mCtx;

public DbAdapter(Context ctx)
{
mCtx = ctx; //set context
}

/// This function adds all the student INFO to the DB
public long addStudentInfoToDb(String stuID,String stuFName, String stuLName) {
ContentValues student_info = new ContentValues();

student_info.put(STU_ID, stuID);
student_info.put(STU_FNAME, stuFName);
student_info.put(STU_LNAME, stuLName);
return mDb.insert("Student", null, student_info);

}

我在另一个名为:LoginScreenActivity 的类/Activity 中使用此信息

我像这样声明 DbAdapter 类... dbHelper = new DbAdapter(LoginScreenActivity.this);

 public void loginScreenFill() {

final EditText student_id = (EditText) findViewById(R.id.student_id_edit);
final EditText student_fname = (EditText) findViewById(R.id.fname_edit);
final EditText student_lname = (EditText) findViewById(R.id.lname_edit);
final EditText course_code = (EditText) findViewById(R.id.classcode_edit);

Button login_button = (Button) findViewById(R.id.login_button);
login_button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String id = String.valueOf((student_id.getText()));
String first_name = String.valueOf(student_fname.getText());
String last_name = String.valueOf(student_lname.getText());
String class_code = String.valueOf(course_code.getText());
dbHelper.addStudentInfoToDb(id, first_name, last_name);




addStudentInfo(id,first_name,last_name);



}

我得到了这个......07-27 19:26:02.211: E/AndroidRuntime(1924): java.lang.NullPointerException07-27 19:09:22.615: E/AndroidRuntime(1113): 在 com.test.quizapp.DbAdapter.addStudentInfoToDb(DbAdapter.java:121)

07-27 19:26:02.211: E/AndroidRuntime(1924): FATAL EXCEPTION: main
07-27 19:26:02.211: E/AndroidRuntime(1924): java.lang.NullPointerException
07-27 19:26:02.211: E/AndroidRuntime(1924): at com.test.quizapp.DbAdapter.addStudentInfoToDb(DbAdapter.java:121)
07-27 19:26:02.211: E/AndroidRuntime(1924): at com.test.quizapp.LoginScreenActivity$1.onClick(LoginScreenActivity.java:54)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.view.View.performClick(View.java:4211)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.view.View$PerformClick.run(View.java:17362)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.os.Handler.handleCallback(Handler.java:725)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.os.Looper.loop(Looper.java:137)
07-27 19:26:02.211: E/AndroidRuntime(1924): at android.app.ActivityThread.main(ActivityThread.java:5227)
07-27 19:26:02.211: E/AndroidRuntime(1924): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 19:26:02.211: E/AndroidRuntime(1924): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 19:26:02.211: E/AndroidRuntime(1924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-27 19:26:02.211: E/AndroidRuntime(1924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-27 19:26:02.211: E/AndroidRuntime(1924): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我明白了。我从来没有打开过sqlitedatabase...

   public void open() throws SQLException{ mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
}

所以在我的代码中我需要DbHelper.open();

关于java - DbHelper 类上下文中出现奇怪的空指针异常,其中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17903801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com