Start a new topic
Answered

getTypedObject returns null if the entity contains an integer field

Hello guys,


I have a problem with getTypedObject returning null here:

     BookCloud c = this.book.getTypedObject(BookCloud.class);

The problem only appears when I add an integer or float field to my class, If I remove that field then works fine. I also can see the correct values in the kinveyreference "obj" field retrieved.

Here is the BookCloud class with an integer "pages" field added:

public class BookCloud extends extends GenericJson {

 @Key private String title;

 @Key private String edition;

 @Key private int pages;

 public BookCloud() {

 }

Can you help me?

Thanks in advance.


Best Answer

Hi,


I was able to reproduce the issue using a similar code snippet and I got following error:

  • I/System.out﹕ unable to instantiate class!
  • W/System.err﹕ java.lang.IllegalArgumentException: field <Class>.status has type int, got java.math.BigDecimal


I was able to solve this error by changing the declaration of the field to Number instead of int/Integer.


Let me know if this works for you.


Regards,

Wani


Answer

Hi,


I was able to reproduce the issue using a similar code snippet and I got following error:

  • I/System.out﹕ unable to instantiate class!
  • W/System.err﹕ java.lang.IllegalArgumentException: field <Class>.status has type int, got java.math.BigDecimal


I was able to solve this error by changing the declaration of the field to Number instead of int/Integer.


Let me know if this works for you.


Regards,

Wani

Hi Ruben,


I need some more details about this so that I can help you find a solution.


If possible, please share

  • A little more detailed code snippet
  • Some details about your use case

Regards,
Wani

Hi Wani,


First, here is the library class which contains an array of librarybooks:


public class LibraryCloud extends GenericJson  { 

 @Key private String name;

    @Key private ArrayList<LibraryBookCloud> librarybooks = new ArrayList<LibraryBookCloud>();

public LibraryCloud() { 

 }

 @Override public Class<LibraryEntity> getEntityClass() { return LibraryEntity.class; } 

    @Override public String[] getResolves() { return new String[]{"librarybooks.book"}; }

 public String getName() { return name; } 

}


Second, the librarybook class containing the KinveyReference to the book


public class LibraryBookCloud extends GenericJson { 

 @Key private String state;

 @Key private KinveyReference book = new KinveyReference();


 public LibraryBookCloud() {

 }

 public String getState() { return state; }

    public void setBook(BookEntity book){

     this.book = new KinveyReference(book.getEntityCloud().getEntityName(), book.getEntityCloud().get("_id").toString());

    }


    public BookCloud getBook() {

     BookCloud c = this.book.getTypedObject(BookCloud.class);

     return c;

    }

}


Finally, here is the call to the backend:

    public void Get(LibraryCloud lc, ArrayList<QueryCondition> query) {

  AsyncAppData<LibraryCloud> mData = mClient.appData("Librarys", LibraryCloud.class);

     mData.get(getKinveyQuery(query), lc.getResolves(), new KinveyListCallback<LibraryCloud>() {

         @Override

         public void onSuccess(LibraryCloud[] r) {

             ArrayList<LibraryCloud> entities = new ArrayList<LibraryCloud>();

          for (LibraryCloud e : r)

           entities.add(e);

       Log.d(App.LOG, r.length + " cloud <Libraries> retrieved");

          if (dsi!=null) dsi.onCloudGetReceived("Libraries", entities);

         }

         @Override

         public void onFailure(Throwable e) {

       Log.e(App.LOG, "failed to get cloud data <Libraries>", e);

          if (dsi!=null) dsi.onCloudGetFailed();

         }

        });

    }


The backend retuns the correct data but in the LibraryBookCloud the next line sets BookCloud c to null...

     BookCloud c = this.book.getTypedObject(BookCloud.class);

...despite I can see the _obj within the book object containing the correct data for the book:


{"_collection":"Books","_id":"b80152b13311485aa7d5a8e0d505c990","_type":"KinveyRef","_obj":{"_id":"b80152b13311485aa7d5a8e0d505c990","author":{"country":"Country1","name":"Author1"},"edition":"Edition1.1","pages":123,"title":"Title1","_acl":{"creator":"548c437944d53dc54300050b"},"_kmd":{"lmt":"2015-04-17T17:21:57.967Z","ect":"2015-04-17T17:21:57.967Z"}}}


As I said before, I've checked that removing the next field from BookCloud class works fine. Also checked that with float fields fails too, looks like only string and booleans works fine.

@Key private int pages;


Thanks for your time,

Ruben.

Hi Wani, attached you can find the details.


Thanks for your time.

java
(3.04 KB)

Hi Wani,


For me it makes no sense to have to change all float and int fields of my entities to Number but just tested and works fine.


Many thanks,

Ruben.

Ruben: 


To give you some insight, we do have this flagged as a bug on our end and it will get attention going forward.  For now using Number rather than the other primitive types and wrapper classes is inconvenient and we apologize that this is the case.


Thanks,

Login or Signup to post a comment