ORMLite索引1处的绑定值为null

时间:2013-03-10 13:29:33

标签: android ormlite

我正在尝试在LyricsInfo类中向ForeignCollection添加对象,但遗憾的是我收到错误:“索引1处的绑定值为null ”。有任何想法吗?我已经读过查询的selectionArg argument可能导致此异常。

03-10 14:07:35.545: W/System.err(26781): java.lang.IllegalStateException: Could not build lazy iterator for class com.ep45.db.Lyric
03-10 14:07:35.545: W/System.err(26781):    at com.j256.ormlite.dao.LazyForeignCollection$1.closeableIterator(LazyForeignCollection.java:88)
03-10 14:07:35.545: W/System.err(26781):    at com.j256.ormlite.dao.CloseableWrappedIterableImpl.closeableIterator(CloseableWrappedIterableImpl.java:32)
03-10 14:07:35.545: W/System.err(26781):    at com.j256.ormlite.dao.CloseableWrappedIterableImpl.iterator(CloseableWrappedIterableImpl.java:22)
03-10 14:07:35.545: W/System.err(26781):    at com.j256.ormlite.dao.CloseableWrappedIterableImpl.iterator(CloseableWrappedIterableImpl.java:12)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.db.LyricsInfo.getLyrics(LyricsInfo.java:34)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.db.LyricsInfo.<init>(LyricsInfo.java:82)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.audio.AudioHelper.getSongInfo(AudioHelper.java:228)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.activities.MainscreenActivity.loadNewSongs(MainscreenActivity.java:287)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.activities.MainscreenActivity_.access$5(MainscreenActivity_.java:1)
03-10 14:07:35.545: W/System.err(26781):    at com.ep45.activities.MainscreenActivity_$12.run(MainscreenActivity_.java:363)
03-10 14:07:35.550: W/System.err(26781):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-10 14:07:35.550: W/System.err(26781):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-10 14:07:35.550: W/System.err(26781):    at java.lang.Thread.run(Thread.java:856)
03-10 14:07:35.550: W/System.err(26781): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
03-10 14:07:35.550: W/System.err(26781):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:297)
03-10 14:07:35.550: W/System.err(26781):    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:401)
03-10 14:07:35.550: W/System.err(26781):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:55)
03-10 14:07:35.555: W/System.err(26781):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-10 14:07:35.555: W/System.err(26781):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1693)
03-10 14:07:35.555: W/System.err(26781):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1667)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:162)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:232)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.dao.BaseDaoImpl.createIterator(BaseDaoImpl.java:952)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.dao.BaseDaoImpl.iterator(BaseDaoImpl.java:531)
03-10 14:07:35.555: W/System.err(26781):    at com.j256.ormlite.dao.BaseDaoImpl.iterator(BaseDaoImpl.java:526)
03-10 14:07:35.560: W/System.err(26781):    at com.j256.ormlite.dao.LazyForeignCollection.seperateIteratorThrow(LazyForeignCollection.java:75)
03-10 14:07:35.565: W/System.err(26781):    at com.j256.ormlite.dao.LazyForeignCollection$1.closeableIterator(LazyForeignCollection.java:86)
03-10 14:07:35.565: W/System.err(26781):    ... 12 more

LyricsInfo.java

public class LyricsInfo {
@DatabaseField(generatedId=true) @Getter @Setter private int id;
@ForeignCollectionField @Setter private ForeignCollection<Lyric> lyrics;
...

public List<Lyric> getLyrics() {
    ArrayList<Lyric> itemList = new ArrayList<Lyric>();
    CloseableWrappedIterable<Lyric> it = lyrics.getWrappedIterable();
    try {
        for(Lyric lyric: it)
            itemList.add(lyric);
    } finally {
        try {
            it.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return itemList;
}

public LyricsInfo(List<String> theLyrics) {
    try {

        if(lyrics == null)
            lyrics = DatabaseManager.getInstance().getHelper().getDao(DatabaseHelper.LYRICSINFO).getEmptyForeignCollection("lyrics");
    } catch (SQLException e) {
        Log.e("Test","Exception occured");
        e.printStackTrace();
    }
    ...
    lyrics.add(...);
    ...

}

}

Lyric.java

@DatabaseTable
@RequiredArgsConstructor()
@NoArgsConstructor()
public class Lyric {
    @DatabaseField(generatedId=true) @Getter @Setter private int id;
    @DatabaseField(canBeNull=true) @NonNull @Getter @Setter private int time;
    @DatabaseField(canBeNull=true) @NonNull @Getter @Setter private String text;
    @DatabaseField(foreign=true,foreignAutoRefresh=true)  @Getter @Setter private LyricsInfo lyricsInfo;


    public void appendText(String text) {
        this.text += text;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您阅读以下两个答案中的任何一个:

他们似乎在说Sqlite正在抛出异常,因为它试图迭代的一个字段是null。它可能是:

@DatabaseField(canBeNull=true) @NonNull @Getter @Setter private int time;

也许那应该是Integer?我只是在猜测。如果您显示导致此问题的数据行,我们可能会提供更具体的帮助。