I'm getting a nullPointerException in my code when i try pull all the columns out of my (small) database. From what I can tell, it's caused by me not having the right Activity context when creating my database. It's great knowing what i'm doing wrong, but i can't see how to fix it! can any of you point me in the right direction?
Here is my method for pulling the data out.
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;
}
Here is the header on my Database class
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();
}
and Here is my Helper class.
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 + ")";
}
}
}
and the crash log (added)
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{http://ift.tt/1WqIDmc}: 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)
I know i'm doing something stupid and just can't see it!!
Aucun commentaire:
Enregistrer un commentaire