2013-07-15 46 views
2

我正在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) 
+1

發表完整的stacktrace和你打開的代碼打開() – njzk2

+0

簡單地說,它無法找到你的數據庫。確保你打開並從正確的路徑。 – Broak

+0

@ njzk2什麼是stacktrace,我在哪裏可以在eclipse中找到它?您是否正在關注LogCat中的錯誤消息? – user202

回答

1

回答的問題,如通過njzk2在註釋中給出,

在將對DBAdapter類的onCreate()方法,參數從爲arg0改變爲DB。您當前正在對未初始化的另一個對象執行查詢。

0

調用db.execSQL前功能在你的onCreate方法中,請致電 db = openOrCreateDatabase("MyDBPath", MODE_PRIVATE, null);。如果數據庫不存在,該方法將創建數據庫,如果數據庫存在,則將其打開。

有關該功能的完整說明,請參閱documentation

+0

我應該寫什麼而不是「MyDBPath」? – user202

+0

不同意。由於調用openOrCreateDatabase(它是一個Context類)或getWritableDatabase,因此調用onCreate。我從不使用openOrCreateDatabase。 – njzk2

+0

你可以使用類似「MyDB」的東西。它只是引用保存在本地文件系統上的數據庫。 – blaffie

相關問題