0

I am trying to create a android app using GreenDAO, this is ERP kind of project so practically it is not possible to post all code here, but i am going to share only related code

    QueryBuilder<PartyCommunication> partyCommQueryBuilder = partyCommunicationDao.queryBuilder();
   partyCommQueryBuilder = partyCommQueryBuilder.where(
        PartyCommunicationDao.Properties.CommType.eq("ALERT"), 
        PartyCommunicationDao.Properties.ReferenceCategory.eq("Low Stock"));
   List<PartyCommunication> listOfPartyComm = partyCommQueryBuilder.list();

   PartyCommunication daoPartyCommunication = listItr.next();
   Long reference_entity_key = daoPartyCommunication.getReferenceEntityKey();       
   Product product = daoSessionUni.getProductDao().load(reference_entity_key);
   ProductDetail productDetail = new ProductDetail(product);
   Integer inventoryQOH= productDetail.getInventoryQOH(); 

I am getting exception in this line

  Product product = daoSessionUni.getProductDao().load(reference_entity_key);

When i debug our application i found that it throwing exception from one of our DAO class as below

         public Product readEntity(Cursor cursor, int offset) {
    Product entity = new Product( //
        .
        .
        .
        .
        cursor.getShort(offset + 23) != 0, // isSync
        cursor.getShort(offset + 24) != 0, // isDeleted
        cursor.getString(offset + 25), // createdBy
        cursor.getString(offset + 26), // modifiedBy
        cursor.isNull(offset + 27) ? null : new java.util.Date(cursor.getLong(offset + 27)), // lastSyncTime
        new java.util.Date(cursor.getLong(offset + 28)), // created
        new java.util.Date(cursor.getLong(offset + 29)) // modified
    );
    return entity;
}

In this line

  cursor.getString(offset + 25), // createdBy

This is our 25th column of table.I know that all code is not enough to understand what is going wrong and why i am getting this exception , so i am also going to post logcat output

     java.lang.IllegalStateException: get field slot from row 0 col 25 failed
at net.sqlcipher.CursorWindow.getString_native(Native Method)
at net.sqlcipher.CursorWindow.getString(CursorWindow.java:382)
at net.sqlcipher.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at com.tarnea.kareeb.model.ProductDao.readEntity(ProductDao.java:273)
at com.tarnea.kareeb.model.ProductDao.readEntity(ProductDao.java:1)
at de.greenrobot.dao.AbstractDao.loadCurrent(AbstractDao.java:417)
at de.greenrobot.dao.AbstractDao.loadUnique(AbstractDao.java:163)
at de.greenrobot.dao.AbstractDao.loadUniqueAndCloseCursor(AbstractDao.java:150)
at de.greenrobot.dao.AbstractDao.load(AbstractDao.java:139)
at com.tarnea.android.DataCleaningService.adjustLowStockAlertTable(DataCleaningService.java:115)
at com.tarnea.sync.kareeb.pharma.syncadapter.SyncManager.performSync(SyncManager.java:970)
at com.tarnea.sync.kareeb.pharma.syncadapter.SyncAdapter.onPerformSync(SyncAdapter.java:96)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254

Thanks in advance to all.If anybody have some interest to solve my problem then please please ask any further information whatever you want.

CL.
  • 173,858
  • 17
  • 217
  • 259
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • Show the query for the cursor. – CL. Aug 18 '14 at 09:01
  • Thanks for your response. I update my question. Please have a look and help me. – DJhon Aug 18 '14 at 09:06
  • @CL....Any idea what is wrong with above code...?? – DJhon Aug 18 '14 at 09:18
  • I don't know anything about the internals of GreenDAO. But why do you think that the cursor has 25 columns? – CL. Aug 18 '14 at 09:22
  • Because this is our DB schema.... – DJhon Aug 18 '14 at 09:26
  • Is `offset` representing the row in your `ResultSet`? If so, you should be using `Cursor.move(int offset)` or `Cursor.moveToNext()` instead. `Cursor.getShort()` takes the number of the column in a single row. – diginoise Aug 18 '14 at 09:34
  • offset represent column of our table – DJhon Aug 18 '14 at 09:42
  • @CL... I got one cause but i am not sure can you suggest me..it is correct or not...?? – DJhon Aug 18 '14 at 10:23
  • Do you have defined `Property`s of all those columns? – CL. Aug 18 '14 at 10:26
  • yes... public final static Property CreatedBy = new Property(25, String.class, "createdBy", false, "created_by"); – DJhon Aug 18 '14 at 10:28
  • Did you check, if the databasefile that you are actually using has all columns you expect? I worked a lot with greendao and I also know the most important internals of it. Such errors alway are based in incorrect, forgotten or non-working schema-updates. (see my answer) – AlexS Aug 23 '14 at 21:39
  • @AlexS... Thanks alex... and sorry for late replay.. I will check it and .. Let you know... – DJhon Aug 24 '14 at 14:38

1 Answers1

0

Your problem isn't the query. The SQLite-Table simply does only contain 24 columns.

Probably you modified your database schema by adding new columns like createdBy.

  1. Either you forgot to update your db using OpenHelper
  2. Or you had an error in your ALTER TABLE-statements
  3. Or you ran your app with the new schema-version but without your ALTER TABLE-statements yet included (which is practically the same as 1.

Have a look at your database using SqliteManager, to verify your tables have the columns you expect.

If you cannot get your database-file you can simply delete your app with all data or increase your schema-version and use the DevOpenHelper to recreate the whole database.

AlexS
  • 5,295
  • 3
  • 38
  • 54