我想将一些数据保存到sqlite数据库但是发生错误NullpointerException,我无法找到此错误请告诉我
DBHandler.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();
}
}
RegistrationActivity
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)
答案 0 :(得分:2)
替换
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"));
答案 1 :(得分:2)
您的代码中有4个错误..
1.您不会在代码中的任何位置创建表格。
在帮助类<{p>>的onCreate()
方法中添加此行
db.execSQL(CREATE_TABLE_USER);
您只是打印了日志消息,但没有创建任何表..
像这样改变你的方法..
@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,"
+ ")";
db.execSQL(CREATE_TABLE_USER);
Log.d(LOGCAT,"CREATE_TABLE_USER Created");
}
2.像这样改变您的代码..
values.put(USER_MOBILE_NUMBER, queryValues.get("contact"));
3.还有一项对代码的修改......您正在将数据插入到错误的表名中。。
database.insert("CREATE_TABLE_USER ", null, values);
在这里,您必须通过USER_TABLE
CREATE_TABLE_USER
个受托人
database.insert(USER_TABLE, null, values);
4.context仅在活动中声明未初始化,因此替换
handler = new DBHandler(context);
与
handler = new DBHandler(RegistrationActivity.this);
答案 2 :(得分:1)
您正尝试使用HaspMap
试试如下:
values.put(USER_MOBILE_NUMBER, queryValues.get("contact"));
此外,您只编写了一个用于创建表但未执行它的查询,因此它可能没有创建表。并在onCreate()
之外编写表查询尝试在onCreate()
中执行create table查询,如下所示:
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,"
+ ")";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.d(LOGCAT,"CREATE_TABLE_USER Created");
db.execSQL(CREATE_TABLE_USER);
}
context
课程的RegistrationActivity
中DBHandler
为空。
在onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
context=RegistrationActivity.this;
handler = new DBHandler(context);
答案 3 :(得分:0)
这可能是导致NullPointerException的问题 你在HashMap中加入了“contact”,但从中得到“mobileNo”
queryValues.put("contact", contactNo);
values.put(USER_MOBILE_NUMBER, queryValues.get("mobileNo"));