2015-11-01 71 views
-1

當我嘗試從我的(小)數據庫中拉出所有列時,我在代碼中收到nullPointerException。從我所知道的情況來看,這是由於我在創建數據庫時沒有正確的活動上下文而導致的。知道我做錯了什麼是很好的,但我看不出如何解決它!你們中的任何一個人都可以指出我的方向嗎?問題與活動上下文和SQLite

這裏是我拉出數據的方法。

public ArrayList<Task> getAllTasks() throws ParseException { 
    ArrayList<Task> tempArraList = null; 
    Task tempTask = null; 

    String[] cols = {TaskDBContract.TaskEntry.COLUMN_NAME_TASK_ID, 
      TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION, 
      TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME, 
      TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY 
    }; 
    String selection = "*"; 
    String[] selectionArgs = null; 
    String groupBy = null; 
    String having = null; 
    String orderBy = null; 



    Cursor c = database.query(
      TaskDBContract.TaskEntry.TABLE_NAME, 
      cols, 
      selection, 
      selectionArgs, 
      groupBy, 
      having, 
      orderBy 
    ); 
    int index = 1; 
    while (c.moveToNext()){ 

     if (index == 0){ tempTask.location = c.getInt(c.getPosition());} 
     if (index == 1){ tempTask.task = c.getString(c.getPosition());} 
     if (index == 2){ tempTask.date.setTime(sdf.parse(c.getString(c.getPosition())));} 
     if (index == 3){ 
      tempTask.category = c.getString(c.getPosition()); 
      tempArraList.add(tempTask); 
     } 

     index ++; 
     if (index == 4){index = 0;}; 

    } 
    c.close(); 

    return tempArraList; 
} 

這裏是我的數據庫類

public class TaskDatabase { 
public static final String DATABASE_NAME = "Taskdb"; 
private static final String COMMA_SEP = ","; 
private static final String TAG = "DatabaseConnector"; 
private SQLiteDatabase database; 
private taskDBHelper dbOpenHelper; 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d kk:mm", Locale.getDefault()); 

public TaskDatabase(Context context) { 
    dbOpenHelper = new taskDBHelper(context, DATABASE_NAME); 
} 

public void openDatabaseConnection() throws SQLException 
{ 
    //open database in reading/writing mode 
    database = dbOpenHelper.getWritableDatabase(); 
} 

public void closeDatabaseConnection() throws SQLException 
{ 
    if (database != null) 
     database.close(); 
} 

,這裏是我的助手類的頭。

public class taskDBHelper extends SQLiteOpenHelper { 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d kk:mm", Locale.getDefault()); 
private static final String TEXT_TYPE = " TEXT"; 
private static final String COMMA_SEP = ","; 
private static final String SQL_CREATE_ENTRIES = 
     "CREATE TABLE " + TaskDBContract.TaskEntry.TABLE_NAME + " (" + 
       TaskDBContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
       TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + TEXT_TYPE + COMMA_SEP + 
       TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + TEXT_TYPE + COMMA_SEP + 
       TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + TEXT_TYPE + COMMA_SEP + ")"; 

private static final String SQL_DELETE_ENTRIES = 
     "DROP TABLE IF EXISTS " + TaskDBContract.TaskEntry.TABLE_NAME; 
// If you change the database schema, you must increment the database version. 


public static final int DATABASE_VERSION = 1; 


public taskDBHelper(Context context, String name) { 
    super(context, name, null, DATABASE_VERSION); 
} 

public taskDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { 
    super(context, name, factory, DATABASE_VERSION); 
} 

public void onCreate(SQLiteDatabase db) { 
    db.execSQL(SQL_CREATE_ENTRIES); 
} 

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // This database is only a cache for online data, so its upgrade policy is 
    // to simply to discard the data and start over 
    db.execSQL(SQL_DELETE_ENTRIES); 
    onCreate(db); 
} 

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    onUpgrade(db, oldVersion, newVersion); 
} 

public String insertTask(Task temptask) { 
    sdf.setCalendar(temptask.date); 


    String SQL_TASK_INSERT = "INSERT INTO " + TaskDBContract.TaskEntry.TABLE_NAME + 
      "(" + TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + COMMA_SEP + 
      TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + COMMA_SEP + 
      TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + COMMA_SEP + ") VALUES (" + 
      temptask.task + COMMA_SEP + sdf.format(temptask.date) + COMMA_SEP + 
      temptask.category + ")"; 

    return SQL_TASK_INSERT; 
} 

public void InsertTaskArray(ArrayList<Task> taskArrayList) { 


    for (int j = 0; j < taskArrayList.size(); j++) { 
     sdf.setCalendar(taskArrayList.get(j).date); 

     String SQL_TASK_INSERT = "INSERT INTO " + TaskDBContract.TaskEntry.TABLE_NAME + 
       "(" + TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + COMMA_SEP + 
       TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + COMMA_SEP + 
       TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + COMMA_SEP + ") VALUES (" + 
       taskArrayList.get(j).task + COMMA_SEP + sdf.format(taskArrayList.get(j).date) + COMMA_SEP + 
       taskArrayList.get(j).category + ")"; 
    } 

} 

}

和崩潰日誌(添加)

11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: FATAL EXCEPTION: main 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: Process: com.example.balefire.test, PID: 9032 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.balefire.test/com.example.balefire.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:148) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.example.balefire.test.TaskDatabase.getAllTasks(TaskDatabase.java:138) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.example.balefire.test.MainActivity.onCreate(MainActivity.java:72) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:6237) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.-wrap11(ActivityThread.java)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:148)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5417)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   

我知道我在做一些愚蠢的事,只是看不到它!

+0

顯示崩潰日誌和你在哪裏使用'TaskDatabase'類 –

+0

已添加崩潰日誌 – Deviousterran

+0

顯示TaskDatabase類完整代碼 –

回答

1

你必須初始化數據庫對象內部getAllTasks()方法如下圖所示:

SQLiteDatabase database; 
taskDbHelper dbhelper; 
dbhelper = new taskDbHelper(<Activity_Name>.this,TaskDatabase.DATABASE_NAME); 
database = dbhelper.getReadableDatabase(); 

然後,你可以打電話給你database.query()方法。

希望它有幫助!

+0

@Deviousterran做得很好嗎? – Droidwala