简体   繁体   中英

Null Object Reference when trying to call a method from a list of objects extracted from sqlite

I am having trouble diagnosing an error after I extract some data from an sqlite database and pass it to another class. I think the passing is where I have the problem.

Fact: My database is filled with data, of the following format:

1447206226445|1
1447206228288|0
1447206462437|1

(First column is a long, second column is an integer)

In my main class, I am trying to search the database for a range of long values, and the display things accordingly. I use:

Days[] daysList = dbHandler.findRange(first.getTimeInMillis(), last.getTimeInMillis());

to get a list of dates that I want, where first and last are Calendar objects.

I then use:

for (int i = 0; i < 7; i++) {
    for (int j = 0; j < daysList.length; j++) {
        if (daysList[j].getID() > first.getTimeInMillis() && daysList[j].getID() < second.getTimeInMillis()) {
        ...
    ...
...

to sort through the data in daysList. However, I get the following error here:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.example.william.timeclockr.Days.getID()' on a null object reference

Here is my DBHandler findRange() method:

public Days[] findRange(long startRange, long endRange) {
    String query = "Select * FROM " + TABLE_DAYS + " WHERE " + COLUMN_DAYSLONG + " >= " + startRange + " AND " + COLUMN_DAYSLONG + " <= " + endRange + ";";

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(query, null);

    Days day = new Days();
    Days[] days = new Days[cursor.getCount()];
    int i = 0;
    cursor.moveToFirst();

    do {
    //while (cursor.moveToNext()) {
    day.setID(cursor.getLong(0));
    day.setStatus(cursor.getInt(1));
    days[i] = day;
    i++;
    cursor.moveToNext();
    //}
    } while (cursor.moveToNext());

    cursor.close();
    db.close();
    return days;
}    

And here is my Days class:

package com.example.william.timeclockr;

public class Days {

    private long _id;
    private int _status;

    public Days() {

    }

    public Days(long id, int status) {
        this._id = id;
        this._status = status;
    }

    public Days(int status) {
        this._status = status;
    }

    public void setID(long id) {
        this._id = id;
    }

    public long getID() {
        return this._id;
    }

    public void setStatus(int status) {
        this._status = status;
    }

    public int getStatus() {
        return this._status;
    }
}

I know this is a lot of information, but I feel like I am making a really rookie mistake passing that list around, any help?

The problem appears to be all in your do-while loop in the findRange() method. You need to instantiate a new Days object each time through the loop. Also, you're calling cursor.moveToNext(); twice each time through, which is advancing the Cursor more quickly than the array index, causing the eventual NullPointerException .

do {
    day = new Days();
    day.setID(cursor.getLong(0));
    day.setStatus(cursor.getInt(1));
    days[i] = day;
    i++;
} while (cursor.moveToNext());

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.

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