大家好久以后我在这里发布任何查询。我在以下代码中收到NullPointer异常错误: -
public class DBAdapter {
int id = 0;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "FirstName";
public static final String KEY_LNAME = "LastName";
public static final String KEY_EMAILID = "Email";
public static final String KEY_PASSWORD = "Password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "UserDetail";
private static final String DATABASE_TABLE = "tblUserDetail";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table tblUserDetail (_id integer primary key autoincrement, "
+ "FirstName text not null, LastName text, Email text not null, Password text not null );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database version from"+ oldVersion +
"to"+ newVersion +", which will destroy all old data");
db.execSQL("DROP TABLE IF EXIST tblUserDetail");
onCreate(db);
}
}
public DBAdapter open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertData(String fName,String lName, String emailId, String password){
ContentValues intialValues = new ContentValues();
intialValues.put(KEY_FNAME, fName);
intialValues.put(KEY_LNAME, lName);
intialValues.put(KEY_EMAILID, emailId);
intialValues.put(KEY_PASSWORD, password);
return db.insert(DATABASE_TABLE, null, intialValues);
}
public String getAllEntries()
{
SQLiteDatabase database = DBHelper.getReadableDatabase();
Cursor cursor = database.rawQuery(
"SELECT FirstName FROM tblUserDetail WHERE _id = 1", null);
if(cursor.moveToFirst()) {
return cursor.getString(0);
}
return cursor.getString(0);
}
}
我点击按钮时调用insertData
和getAllEntries
: -
Submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast toast = Toast.makeText(getApplicationContext(), "cliecked on submit button", Toast.LENGTH_LONG);
// toast.show();
String str;
try{
db.insertData(etFN.getText().toString(), etLN.getText().toString(), etEI.getText().toString(), etPS.getText().toString());
str = db.getAllEntries();
Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG);
toast.show();
}
catch(Exception ex){
Context context = getApplicationContext();
CharSequence text = ex.toString();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
});
请帮我摆脱这个NullPointer Exception Error.Every help将受到高度赞赏。
我收到了db.insertData()
行的错误。
答案 0 :(得分:1)
这可能有两个原因。
1)如果您尚未初始化 DBADapter 对象。你刚宣布了它。
所以初始化
db = new DBAdapter(this);
2)在将数据插入数据库之前,您尚未打开或关闭数据库。因此,首先打开数据库,然后再将数据插入其中。
谢谢!