I am trying to get book data from my database but I keep running into a NullPointerException. I have a double array Entries that I want filled with a book item and its 5 corresponding details. I know I have extra elements in the InventoryAdapter array that I dont use in this method, but I keep it there since I use it elsewhere.
The error occurs at Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1];
and by extension at Cursor cursor = db.rawQuery(query, new String[] {ISBN});
Why would the error be at the Cursor? I would have thought the error would probably occur with calling an array location that doesn't exist.
Main code
int numEntries = 2;
Entries = new String[numEntries][5];
int totalCount = 0;
if (BuildConfig.DEBUG)
Log.d(debug.LOG, "numEntries="+numEntries);
Toast.makeText(ReportsByDateScreen.this, "numEntries="+numEntries, Toast.LENGTH_LONG).show();
// Set up search array
for(int i = 0; i < numEntries; i++)
{
Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1];
Entries[i][1] = InventoryAdapter.getInventoryByISBN(records[i][0])[2];
Entries[i][2] = InventoryAdapter.getInventoryByISBN(records[i][0])[4];
Entries[i][3] = InventoryAdapter.getInventoryByISBN(records[i][0])[3];
for(int j = 0; j < records.length; j++)
{
if(records[j][0].equals(InventoryAdapter.getInventoryByISBN(records[i][0])[0]))
{
totalCount+=Integer.parseInt(InventoryAdapter.getInventoryByISBN(records[i][0])[8]);
}
}
Entries[i][4] = ((Integer)totalCount).toString();
reportArray.add(new ReportsItem(records[i][0],Entries[i]));
totalCount = 0;
}
InventoryAdapter
public static String[] getInventoryByISBN(String ISBN)
{
String[] entry = new String[9];
//Query
String query = "select * from INVENTORY where ISBN = ?";
Cursor cursor = db.rawQuery(query, new String[] {ISBN});
if(cursor.getCount()<1) // title Not Exist
{
cursor.close();
for(int i = 0; i < 9; i++)
entry[i] = "Not Found";
return entry;
}
cursor.moveToFirst();
//put data into respective variable
int publish = cursor.getInt(cursor.getColumnIndex("PUBLISH_DATE"));
String publishdate = ((Integer)publish).toString();
String title = cursor.getString(cursor.getColumnIndex("TITLE"));
String author = cursor.getString(cursor.getColumnIndex("AUTHOR"));
String callNumber = cursor.getString(cursor.getColumnIndex("CALL_NUMBER"));
int available = cursor.getInt(cursor.getColumnIndex("AVAILABLE_COUNT"));
String availablecount = ((Integer)available).toString();
int inventory = cursor.getInt(cursor.getColumnIndex("INVENTORY_COUNT"));
String inventorycount = ((Integer)inventory).toString();
int due = cursor.getInt(cursor.getColumnIndex("DUE_PERIOD"));
String dueperiod = ((Integer)due).toString();
int checkoutcount = cursor.getInt(cursor.getColumnIndex("COUNT"));
String count = ((Integer)checkoutcount).toString();
//combine variables into one array
entry[0] = ISBN;
entry[1] = title;
entry[2] = author;
entry[3] = publishdate;
entry[4] = callNumber;
entry[5] = availablecount;
entry[6] = inventorycount;
entry[7] = dueperiod;
entry[8] = count;
cursor.close();
return entry;
}
There is 2 different possibilities here :
I'll look into that if I were you. Use the debugger.
I can tell you much because you don't post the code of your init of the SQLiteDatabase
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.