Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1 #86

Open
MrChriZ opened this issue Oct 12, 2017 · 8 comments

Comments

@MrChriZ
Copy link

MrChriZ commented Oct 12, 2017

 List<ServiceBooking> allServiceBookingsFromVehicles = new ArrayList<>();
        for (Vehicle vehicle : vehicleList)
        {
            if (vehicle.getServiceBookings() !=null && vehicle.getServiceBookings().size()>0 )
            {
                allServiceBookingsFromVehicles.addAll(vehicle.getServiceBookings());
            }
        }

I have some code as above. I'm getting strange crashes from Ormlite out in the wild.
ServiceBookings are a lazy foreign collection.

I'm getting the following error and stack trace from the allServiceBookingsFromVehicles.addAll(vehicle.getServiceBookings());
line above:

android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
Stack Trace:
android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
android.database.AbstractWindowedCursor.getShort(AbstractWindowedCursor.java:62)
com.j256.ormlite.android.AndroidDatabaseResults.getShort(AndroidDatabaseResults.java:172)
com.j256.ormlite.android.AndroidDatabaseResults.getByte(AndroidDatabaseResults.java:162)
com.j256.ormlite.db.BaseDatabaseType$BooleanNumberFieldConverter.resultToSqlArg(BaseDatabaseType.java:613)
com.j256.ormlite.field.BaseFieldConverter.resultToJava(BaseFieldConverter.java:26)
com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:841)
com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:61)
com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:284)
com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:168)
com.j256.ormlite.stmt.SelectIterator.next(SelectIterator.java:181)
com.j256.ormlite.dao.LazyForeignCollection.toArray(LazyForeignCollection.java:216)
java.util.ArrayList.addAll(ArrayList.java:188)

Any guesses as to what I'm doing wrong? The issue isn't easily repeatable unfortunately.

@j256
Copy link
Owner

j256 commented Nov 20, 2017

Are you using hasNext() here? Is it possible that you are running off the end of the results?

@j256
Copy link
Owner

j256 commented Feb 18, 2018

Feel free to reopen if you have more info.

@j256 j256 closed this as completed Feb 18, 2018
@kyawminthu
Copy link

I am facing the same issue.

CloseableIterator<EnConversationThread> iterator = enctDao.iterator(qBuilder.prepare());
while (iterator.hasNext()) {
    EnConversationThread ecThread = iterator.next();

When I call next() in while loop, it throws
"android.database.CursorIndexOutOfBoundsException: Index 3 requested, with a size of 3"

@MrChriZ
Copy link
Author

MrChriZ commented Mar 28, 2018

I think I'd worked around the problem in some kind of hacky way by the time Grey commented here - although I'm not currently working on the project to check.
In answer to:
Are you using hasNext() here? Is it possible that you are running off the end of the results?

In my case the .next is being done by OrmLites internal code as I'm simply iterating over a foreign collection:
com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:168)
com.j256.ormlite.stmt.SelectIterator.next(SelectIterator.java:181)
com.j256.ormlite.dao.LazyForeignCollection.toArray(LazyForeignCollection.java:216)

So I'm not sure how I could go off the end.

@kyawminthu
Copy link

I use hasNext().
next() will be called only when iterator.hasNext() return true.
Full error stack trace of ORM Lite is as follow.
02-14 17:35:22.808 W/System.err( 4583): android.database.CursorIndexOutOfBoundsException: Index 3 requested, with a size of 3
02-14 17:35:22.811 W/System.err( 4583): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
02-14 17:35:22.811 W/System.err( 4583): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-14 17:35:22.811 W/System.err( 4583): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.android.AndroidDatabaseResults.getString(AndroidDatabaseResults.java:134)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.field.types.StringType.resultToSqlArg(StringType.java:39)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.field.BaseFieldConverter.resultToJava(BaseFieldConverter.java:24)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:819)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:60)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161)
02-14 17:35:22.811 W/System.err( 4583): at com.j256.ormlite.stmt.SelectIterator.next(SelectIterator.java:173)

I used 'com.j256.ormlite:ormlite-android:4.48' for my project.

@j256 j256 reopened this Jun 12, 2018
@ElizavetaGorbunova
Copy link

I'm facing the same issue. It occurs randomly. Version com.j256.ormlite:ormlite-android:5.0

@DatabaseTable(tableName = TABLE_NAME)
public class DBAccount {
  @ForeignCollectionField(columnName = USERS)
  private transient ForeignCollection<DBUser> users;
  //...
  public List<DBUser> getUserList() {
          final List<DBUser> userList = new ArrayList<>();
          if (users != null) {
             userList.addAll(users);  // Exception
          }
          return userList;
      }
  }
}

Stacktrace:
android.database.CursorIndexOutOfBoundsException
Index 1 requested, with a size of 1

android.database.AbstractCursor.checkPosition (AbstractCursor.java:468) android.database.AbstractWindowedCursor.isNull (AbstractWindowedCursor.java:92)
com.j256.ormlite.android.AndroidDatabaseResults.wasNull (AndroidDatabaseResults.java:218)
com.j256.ormlite.field.FieldType.resultToJava (FieldType.java:856)
com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow (BaseMappedQuery.java:61)
com.j256.ormlite.stmt.SelectIterator.getCurrent (SelectIterator.java:284)
com.j256.ormlite.stmt.SelectIterator.nextThrow (SelectIterator.java:168)
com.j256.ormlite.stmt.SelectIterator.next (SelectIterator.java:181)
com.j256.ormlite.dao.LazyForeignCollection.toArray (LazyForeignCollection.java:216)
 java.util.ArrayList.addAll (ArrayList.java:588)

@thedrugsthatdeceiveyou
Copy link

I have the same problem:
MainActivityandroid.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1

Is there some workaround? Thanks

@XandrMaster
Copy link

XandrMaster commented Jun 20, 2019

Very similar issue :

Fatal Exception: java.lang.IllegalStateException
at com.j256.ormlite.stmt.SelectIterator.next + 191(SelectIterator.java:191)
       at com.j256.ormlite.dao.LazyForeignCollection.toArray + 216(LazyForeignCollection.java:216)
       at java.util.ArrayList.addAll + 588(ArrayList.java:588)


  @ForeignCollectionField(columnName = MEMBERSHIPS)
  public ForeignCollection<DBMembership> memberships;
  public List<DBMembership> membershipList;
....
public List<DBMembership> getMembershipList() {
		if (membershipList == null) {
			membershipList = new ArrayList<>();
			if (memberships != null) {
				membershipList.addAll(memberships); // CRASH
			}
		}
		return membershipList;
	}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants