我正在android中製作一個簡單的應用程序。我不能夠運行該程序時,logcat中顯示了這個錯誤:Android中的NullPointerException同時獲取可寫數據庫
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.tasks/com.sample.tasks.ToDo}: java.lang.NullPointerException
後的考試,它看起來像他們這種說法的問題:
db = DBHelper.getWritableDatabase();
或本聲明:
final String DATABASE_CREATE = "CREATE TABLE tasks(sno INTEGER PRIMARY KEY ASC, todo TEXT);";
db.execSQL(DATABASE_CREATE);
以下是完整的代碼:
public class DBAdapter {
public static final String KEY_ROWID = "SNo";
public static final String KEY_TASKS = "Tasks";
private static final String DATABASE_NAME = "Records";
private static final String DATABASE_TABLE = "Tasks";
private static final int DATABASE_VERSION = 1;
private final Context context;
private DbHelper DBHelper;
private static SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DbHelper(context);
}
private static class DbHelper extends SQLiteOpenHelper
{
DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
db.execSQL("Create Table " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_TASKS + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
System.out.println("Updating the Database");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
System.out.println("In close");
}
public long createEntry(String task){
.....
}
public String[] getData() {
.....
}
}
我試過尋找未初始化的對象(因此爲空),但找不到任何東西。請幫助我,我錯過了什麼?
UPDATE:
這裏是所涉及的其他文件的主要部分:
public class ToDo extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.to_do);
String[] tasks = new String[16];
//Retrive tasks from database and view it on the screen
DBAdapter info = new DBAdapter(this);
info.open();
tasks = info.getData();
info.close();
}
}
下面是完整的錯誤消息:
07-15 16:11:19.187: E/AndroidRuntime(914): FATAL EXCEPTION: main
07-15 16:11:19.187: E/AndroidRuntime(914): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.tasks/com.sample.tasks.ToDo}: java.lang.NullPointerException
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.os.Looper.loop(Looper.java:123)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 16:11:19.187: E/AndroidRuntime(914): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 16:11:19.187: E/AndroidRuntime(914): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 16:11:19.187: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 16:11:19.187: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 16:11:19.187: E/AndroidRuntime(914): at dalvik.system.NativeStart.main(Native Method)
07-15 16:11:19.187: E/AndroidRuntime(914): Caused by: java.lang.NullPointerException
07-15 16:11:19.187: E/AndroidRuntime(914): at com.sample.tasks.DBAdapter$DbHelper.onCreate(DBAdapter.java:46)
07-15 16:11:19.187: E/AndroidRuntime(914): at com.sample.tasks.DBAdapter.open(DBAdapter.java:60)
07-15 16:11:19.187: E/AndroidRuntime(914): at com.sample.tasks.ToDo.onCreate(ToDo.java:59)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 16:11:19.187: E/AndroidRuntime(914): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
發表完整的stacktrace和你打開的代碼打開() – njzk2
簡單地說,它無法找到你的數據庫。確保你打開並從正確的路徑。 – Broak
@ njzk2什麼是stacktrace,我在哪裏可以在eclipse中找到它?您是否正在關注LogCat中的錯誤消息? – user202