gpt4 book ai didi

android - 我的 sqlite 数据库上的 NullpointerException 和 getWritable() 错误

转载 作者:行者123 更新时间:2023-11-29 15:18:45 27 4
gpt4 key购买 nike

我想将一些数据保存到sqlite数据库中,但是发生了错误NullpointerException,我找不到这个错误请告诉我

数据库处理器.java

public class DBHandler extends SQLiteOpenHelper{

/*public static final String TABLE_USER = "USERTABLE";
public static final String USER_NAME = "username";
public static final String PASSWORD = "password";
public static final String CONFIRM_PASSWORD = "confirm_password";
public static final String MOBILE_NO = "mobile_no"; */


// DataBase Name
public final static String DATABASE = "UserDetails";

// DataBase Table Name's
public final static String USER_TABLE = "TABLE_USER";

// Field names of MALLS Table

public static final String USER_NAME = "username";
public static final String PASSWORD = "password";
public static final String CONFIRM_PASSWORD = "confirmpassword";
public static final String USER_MOBILE_NUMBER = "mobilenumber";
public static final String USER_EMAIL = "email";


private static final String LOGCAT=null;

public DBHandler(Context context) {
super(context, "androidsqlite.db",null, 1);
// TODO Auto-generated constructor stub
Log.d(LOGCAT,"Created");
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

String CREATE_TABLE_USER;
CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
+ USER_TABLE+ " (" + USER_NAME
+ " VARCHAR," + PASSWORD + " VARCHAR,"
+ CONFIRM_PASSWORD + " VARCHAR," + USER_MOBILE_NUMBER + " VARCHAR,"
+ USER_EMAIL+ " VARCHAR,"
+ ")";
Log.d(LOGCAT,"CREATE_TABLE_USER Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String query;
query = "DROP TABLE IF EXISTS CREATE_TABLE_USER";
db.execSQL(query);
onCreate(db);

}
public void insertUserDetails(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USER_NAME, queryValues.get("username"));
values.put(PASSWORD, queryValues.get("password"));
values.put(CONFIRM_PASSWORD, queryValues.get("confirmPwd"));
values.put(USER_MOBILE_NUMBER, queryValues.get("mobileNo"));

database.insert("CREATE_TABLE_USER ", null, values);
database.close();

}

}

注册 Activity

public class RegistrationActivity extends Activity {

private Context context;

private EditText userNameEdt;
private EditText passwordEdt;
private EditText cnfrmPwdEdt;
private EditText contactEdt;

private Button submitBtn;
private Button cancelBtn;

private String userName;
private String pswd;
private String cnfrmPwd;
private String contactNo;


DBHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);

userNameEdt = (EditText)findViewById(R.id.userNameedt);
passwordEdt = (EditText)findViewById(R.id.paswordEdit);
cnfrmPwdEdt = (EditText)findViewById(R.id.confirmPaswordEdit);
contactEdt = (EditText)findViewById(R.id.mobileNoedt);

submitBtn = (Button)findViewById(R.id.submitbtn);
cancelBtn = (Button)findViewById(R.id.cancel);

handler = new DBHandler(context);
submitBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

userName = userNameEdt.getText().toString();
pswd = passwordEdt.getText().toString();
cnfrmPwd = cnfrmPwdEdt.getText().toString();
contactNo= contactEdt.getText().toString();

HashMap<String, String> queryValues = new HashMap<String,String>();

queryValues.put("username", userName);
queryValues.put("password", pswd);
queryValues.put("confrmPwd", cnfrmPwd);
queryValues.put("contact", contactNo);

handler.insertUserDetails(queryValues);

}
});


}



}

错误

12-31 11:52:43.867: E/AndroidRuntime(820):  at   
com.example.esecuresystrem.database.DBHandler.insertUserDetails(DBHandler.java:68)
12-31 11:53:54.908: E/AndroidRuntime(867): FATAL EXCEPTION: main
12-31 11:53:54.908: E/AndroidRuntime(867): java.lang.NullPointerException
12-31 11:53:54.908: E/AndroidRuntime(867): at
android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
12-31 11:53:54.908: E/AndroidRuntime(867): at
com.example.esecuresystrem.database.DBHandler.insertUserDetails(DBHandler.java:68)
12-31 11:53:54.908: E/AndroidRuntime(867): at
com.example.esecuresystrem.RegistrationActivity$1.onClick(RegistrationActivity.java:68)
12-31 11:53:54.908: E/AndroidRuntime(867): at
android.view.View.performClick(View.java:3480)

最佳答案

替换

handler = new DBHandler(context);
//context is only declared not initialized in activity

handler = new DBHandler(RegistrationActivity.this);

在DBHabdler的onCreate中

db.execSQL(CREATE_TABLE_USER );  

并改变

values.put(USER_MOBILE_NUMBER, queryValues.get("contact"));

关于android - 我的 sqlite 数据库上的 NullpointerException 和 getWritable() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20853224/

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