java - 尝试从从 sqlite 中提取的对象列表中调用方法时的空对象引用

标签 java android sqlite android-studio android-sqlite

从 sqlite 数据库中提取一些数据并将其传递给另一个类后,我无法诊断错误。我认为传递是我遇到问题的地方。

事实:我的数据库中充满了以下格式的数据:

1447206226445|1
1447206228288|0
1447206462437|1

(第一列是长整数,第二列是整数)

在我的主类中,我试图在数据库中搜索一系列长值,并相应地显示内容。我使用:

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

获取我想要的日期列表,其中 firstlast 是 Calendar 对象。

然后我使用:

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()) {
        ...
    ...
...

对daysList中的数据进行排序。但是,我在这里收到以下错误:

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

这是我的 DBHandler findRange() 方法:

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

这是我的 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;
    }
}

我知道这是很多信息,但我觉得我在传递该列表时犯了一个非常菜鸟的错误,有什么帮助吗?

最佳答案

问题似乎都在您的 do-while 中在 findRange() 中循环方法。您需要实例化一个新的 Days对象每次通过循环。此外,您正在调用 cursor.moveToNext();每次通过两次,这是在推进Cursor比数组索引更快,导致最终的 NullPointerException .

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

关于java - 尝试从从 sqlite 中提取的对象列表中调用方法时的空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643553/

相关文章:

java - 是否可以在 onCreate() 函数中创建不一致的数据?

java - 无法播放该视频进行视频观看

android - 服务器端估计/识别音频文件相似性的框架/算法/库?

mysql - SQL - 检查表中的元素是否有两个其他元素与它位于同一行

java - 从 Runnable 接口(interface)内的另一个程序调用一个程序时出现异常

java - 服务器无法读取全部内容

android - Android SDK 中 FetchedAppGateKeepersManager 中的 Facebook 错误崩溃

c++ - 插入两个相互引用的 SQL 表而无需执行单独的查询?

android - 需要算法建议 : What's the best way to sort TV show episodes?

java - Spring验证不执行