1

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

我有一个名为 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

我这样声明 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.NullPointerException 07-27 19:09:22.615: E/AndroidRuntime(1113): at 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)
4

1 回答 1

0

我想到了。我从来没有打开过sqlite数据库...

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

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

于 2013-07-28T00:40:52.027 回答