我越來越對下面的代碼行SQLite的選擇查詢返回null時,它不應該
attendanceList = dbHelper.getAttendance(surName, name);
兩個姓氏和名字都沒有空一空指針異常,有大小()> 0,attendanceList是ArrayList類型的,在這裏是getAttendance功能:
public ArrayList<Attendance> getAttendance(String surName, String name)
{
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<Attendance> attendanceList = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT * FROM " + ATTENDANCE_TABLE + " WHERE " + COLUMN_SURNAME + " =? AND "
+ COLUMN_NAME + " =?", new String[] {surName, name});
if(cursor .moveToFirst())
{
while(!cursor.isAfterLast())
{
Attendance attendance = new Attendance();
attendance.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
attendance.setName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
attendance.setSurName(cursor.getString(cursor.getColumnIndex(COLUMN_SURNAME)));
attendance.setState(cursor.getString(cursor.getColumnIndex(COLUMN_STATE)));
attendance.setTerm(cursor.getInt(cursor.getColumnIndex(COLUMN_TERM)));
attendance.setSubject(cursor.getString(cursor.getColumnIndex(COLUMN_SUBJECT_NAME)));
attendanceList.add(attendance);
cursor.moveToNext();
}
cursor.close();
db.close();
return attendanceList;
}
else
{
cursor.close();
db.close();
return null;
}
}
有上述功能無語法錯誤,而且我知道,因爲我看到使用DB瀏覽器中的SQLite的ATTENDANCE_TABLE的內容不應該返回null 。有人可以指出我的代碼有什麼問題嗎?我覺得我寫了1 + 1 = 2,並得到一個錯誤..
有你調用getAttendance函數初始化之前dbHelper? – Pavan
是,在onCreate()方法 – Taivna
堆棧跟蹤添加到問題,請 –