簡體   English   中英

Android Null指針異常SQLite

[英]Android Null Pointer Exception SQLite

我正在嘗試從數據庫中獲取書籍數據,但是我一直遇到NullPointerException。 我有一個雙數組條目,我要填充一個書項目及其5個相應的詳細信息。 我知道我在InventoryAdapter數組中有多余的元素,我沒有在此方法中使用它,但是由於我在其他地方使用了它,所以我將其保留在那里。

錯誤發生在Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1]; 並通過擴展在Cursor cursor = db.rawQuery(query, new String[] {ISBN});

為什么會出現游標錯誤? 我以為該錯誤可能會因調用不存在的數組位置而發生。

主要代號

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;
    }

庫存適配器

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;
}

這里有2種不同的可能性:

  • ISBN為空(並且擴展名records [i] [0]也為空);
  • db為空

如果我是你,我會調查的。 使用調試器。

我可以告訴您很多信息,因為您沒有發布SQLiteDatabase的初始化代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM