我正在尝试使用SQLite创建简单的数据库应用程序,但看起来我无法设法获取对创建的数据库的引用。我一直得到NullPointerException,它指向getWriteableDatabase命令。
数据库类:
public class DatabaseHelper extends SQLiteOpenHelper {
public SQLiteDatabase db;
public static final String DATABASE_NAME = "user_database.db";
public static final String DATABASE_TABLE_NAME = "users";
public static final int DATABASE_VERSION = 1;
public static final String USER_ID = "_id";
public static final String USER_NAME = "user_name";
public static final String USER_SURNAME = "user_surname";
private static final String DATABASE_CREATE =
"CREATE TABLE " +
DATABASE_TABLE_NAME +
" ( " +
USER_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " +
USER_NAME +
" TEXT, " +
USER_SURNAME +
" TEXT);";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
newEntry("Lorem", "Ipsum");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHelper.class.getName(),
"Upgrading database from version " +
oldVersion +
" to " +
newVersion);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NAME);
onCreate(db);
}
public Cursor newEntry(String name, String surname)
{
ContentValues cv = new ContentValues();
cv.put(USER_NAME, name);
cv.put(USER_SURNAME, surname);
long id = db.insert(DATABASE_TABLE_NAME, null, cv);
Cursor cursor = db.query(DATABASE_TABLE_NAME, new String[] {USER_ID, USER_NAME, USER_SURNAME}, USER_ID + " = " + id, null, null, null, null);
return cursor;
}
public Cursor selectAll()
{
return db.query(DatabaseHelper.DATABASE_TABLE_NAME, new String[] {USER_ID, USER_NAME, USER_SURNAME}, null, null, null, null, null);
}
}
UI类:
public class Insert extends Activity implements View.OnClickListener {
public DatabaseHelper myHelper;
public Button button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
}
public void onClick(View v)
{
String name, surname;
EditText input;
Cursor cursor;
Intent intent;
input = (EditText) findViewById(R.id.editText1);
name = input.getText().toString();
input = (EditText) findViewById(R.id.editText2);
surname = input.getText().toString();
myHelper = new DatabaseHelper(this);
cursor = myHelper.newEntry(name, surname);
if(cursor.moveToFirst())
{
Toast.makeText(this, "Entry added.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Entry not added.", Toast.LENGTH_LONG).show();
}
cursor.close();
intent = new Intent(this, Main.class);
startActivity(intent);
}
}
logcat的:
02-15 20:12:31.117: E/AndroidRuntime(2678): FATAL EXCEPTION: main
02-15 20:12:31.117: E/AndroidRuntime(2678): Process: com.example.userdatabase, PID: 2678
02-15 20:12:31.117: E/AndroidRuntime(2678): java.lang.NullPointerException
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.example.userdatabase.DatabaseHelper.newEntry(DatabaseHelper.java:66)
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.example.userdatabase.DatabaseHelper.onCreate(DatabaseHelper.java:47)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.example.userdatabase.DatabaseHelper.<init>(DatabaseHelper.java:39)
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.example.userdatabase.Insert.onClick(Insert.java:38)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.view.View.performClick(View.java:4438)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.view.View$PerformClick.run(View.java:18422)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.os.Handler.handleCallback(Handler.java:733)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.os.Handler.dispatchMessage(Handler.java:95)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.os.Looper.loop(Looper.java:136)
02-15 20:12:31.117: E/AndroidRuntime(2678): at android.app.ActivityThread.main(ActivityThread.java:5001)
02-15 20:12:31.117: E/AndroidRuntime(2678): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 20:12:31.117: E/AndroidRuntime(2678): at java.lang.reflect.Method.invoke(Method.java:515)
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-15 20:12:31.117: E/AndroidRuntime(2678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-15 20:12:31.117: E/AndroidRuntime(2678): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
getWritableDatabase()
会触发onCreate()
。假定onCreate()
的{{1}}来电newEntry()
已初始化,但已在作业的右侧
db
仍在执行中,db = this.getWritableDatabase();
仍为空。
使用传入的db
作为SQLiteDatabase
的参数进行数据库设置操作,例如插入默认值。