简体   繁体   English

Android Null指针异常SQLite

[英]Android Null Pointer Exception SQLite

I am trying to get book data from my database but I keep running into a NullPointerException. 我正在尝试从数据库中获取书籍数据,但是我一直遇到NullPointerException。 I have a double array Entries that I want filled with a book item and its 5 corresponding details. 我有一个双数组条目,我要填充一个书项目及其5个相应的详细信息。 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. 我知道我在InventoryAdapter数组中有多余的元素,我没有在此方法中使用它,但是由于我在其他地方使用了它,所以我将其保留在那里。

The error occurs at Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1]; 错误发生在Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1]; and by extension at Cursor cursor = db.rawQuery(query, new String[] {ISBN}); 并通过扩展在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 : 这里有2种不同的可能性:

  • ISBN is null (and by extension records[i][0] is null too); ISBN为空(并且扩展名records [i] [0]也为空);
  • db is null db为空

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 . 我可以告诉您很多信息,因为您没有发布SQLiteDatabase的初始化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM