Start a new topic

getTypedObject doesn't work.

I tried to use getTypedObject as suggested in this comment: https://support.kinvey.com/discussion/comment/200600058/#Comment_200600058

but it is not working with ArrayList.

The getTypedObject always return null !

In order to verify my code I include a test code in UpdateDetailsFragment.java of the Kinvey StatusShare Example:







public void populateViews(){

if (entity.getThumbnail() != null){

image.setImageBitmap(entity.getThumbnail());

}else{

image.setVisibility(View.GONE);

}

text.setText(entity.getText());

author.setText(entity.getAuthorName());



List commentRefs = entity.getComments();

if (commentRefs == null){

return;

}

//***** included test *****

CommentEntity mycomment = commentRefs.get(0).getTypedObject(CommentEntity.class);

//***** included test END ***** => my comment always null



List comments = new ArrayList();



for (KinveyReference kr : commentRefs){

comments.add(kr.getResolvedObject());

}



CommentAdapter adapter = new CommentAdapter(getSherlockActivity(), comments, getSherlockActivity().getLayoutInflater());

commentList.setAdapter(adapter);





}





Always return null from getTypedObject



Thanks,

Hello, I have the same 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, 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.

Hey,



Can you post the code you use to perform the GET operation on AppData? There are a couple variations of this method, and that might be where the issue is.
Edward,

I added the getTypedObject in the StatusShareActivity example from Kinvey, in the UpdateDetailsFragment.java file. So I believe the code that performed the GET is



private void loadUpdates() {

showListView(false);



Query q = getClient().appData(StatusShare.COL_UPDATES, UpdateEntity.class).query();

q.setLimit(10);

q.addSort("_kmd.lmt", AbstractQuery.SortOrder.DESC);





getClient().linkedData(StatusShare.COL_UPDATES, UpdateEntity.class).get(q, new KinveyListCallback() {

@Override

public void onSuccess(UpdateEntity[] result) {



android.util.Log.d(Client.TAG, "Count of updates found: " + result.length);



for (UpdateEntity e : result) {

Log.d(Client.TAG, "result -> " + e.toString());

}

if (getSherlockActivity() == null){

return;

}





((StatusShare)getSherlockActivity()).setShareList(new ArrayList());

((StatusShare)getSherlockActivity()).getShareList().addAll(Arrays.asList(result));



if ( ((StatusShare)getSherlockActivity()).getShareList().size() == 0){

empty.setVisibility(View.VISIBLE);

loading.setVisibility(View.GONE);

} else{

empty.setVisibility(View.GONE);

setAdapter();

}



}





@Override

public void onFailure(Throwable error) {

Log.w(Client.TAG, "Error fetching updates data: " + error.getMessage());

showListView(true);

}

}, null, new String[]{"author", "comments", "author"}, 3, true);





So there are two reasons this method could return null.



First, can you confirm that the reference i resolved? It cannot be cast to a `CommentEntity` if it is null in the first place, and instead of crashing the library will just return null.



A quick way to test this is to add this line right before your test:



Log.i("my app", "is it null -> " + (commentRefs.get(0).get("_obj") == null));



that will log true if the `_obj` field of the `KinveyReference` object is null, which means it hasn't been resolved. If the above that is the case, let me know because the issue might be elsewhere.



If you do see a `false` logged, can you also look for the message:



"unable to instantiate class!"



That will be logged if there is an issue with the JSON parser, which also would be caught and return a null.
Edward,

Here are the answers



04-10 17:25:47.936: I/my app(15235): is it null -> false



04-10 17:25:47.967: I/System.out(15235): unable to instantiate class!







OK, good-- so the issue is that it cannot create a `CommentEntity`.



below that log line, `unable to instantiate class!`, there should be a red stacktrace. If there is, can you paste that as well?
Here you are



04-10 17:25:47.971: W/System.err(15235): at com.google.api.client.json.GenericJson.set(GenericJson.java:95)

04-10 17:25:47.971: W/System.err(15235): at com.google.api.client.json.GenericJson.set(GenericJson.java:40)

04-10 17:25:47.975: W/System.err(15235): at com.google.api.client.util.GenericData.putAll(GenericData.java:138)

04-10 17:25:47.975: W/System.err(15235): at com.kinvey.java.model.KinveyReference.getTypedObject(KinveyReference.java:117)

04-10 17:25:47.975: W/System.err(15235): at com.kinvey.samples.statusshare.fragments.UpdateDetailsFragment.populateViews(UpdateDetailsFragment.java:100)

04-10 17:25:47.975: W/System.err(15235): at com.kinvey.samples.statusshare.fragments.KinveyFragment.onResume(KinveyFragment.java:57)

04-10 17:25:47.975: W/System.err(15235): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)

04-10 17:25:47.979: W/System.err(15235): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)

04-10 17:25:47.979: W/System.err(15235): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)

04-10 17:25:47.979: W/System.err(15235): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)

04-10 17:25:47.979: W/System.err(15235): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)

04-10 17:25:47.982: W/System.err(15235): at android.os.Handler.handleCallback(Handler.java:587)

04-10 17:25:47.982: W/System.err(15235): at android.os.Handler.dispatchMessage(Handler.java:92)

04-10 17:25:47.982: W/System.err(15235): at android.os.Looper.loop(Looper.java:123)

04-10 17:25:47.982: W/System.err(15235): at android.app.ActivityThread.main(ActivityThread.java:3687)

04-10 17:25:47.982: W/System.err(15235): at java.lang.reflect.Method.invokeNative(Native Method)

04-10 17:25:47.986: W/System.err(15235): at java.lang.reflect.Method.invoke(Method.java:507)

04-10 17:25:47.986: W/System.err(15235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)

04-10 17:25:47.986: W/System.err(15235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

04-10 17:25:47.986: W/System.err(15235): at dalvik.system.NativeStart.main(Native Method)

04-10 17:25:50.084: I/StatusBarPolicy(216): BAT. S:2 H:2

04-10 17:25:50.607: W/PowerManagerService(132): Timer 0x3->0x3|0x0

04-10 17:26:00.092: I/StatusBarPolicy(216): BAT. S:2 H:2

04-10 17:26:00.217: I/StatusBarPolicy(216): onSignalStrengthsChanged

Edward,

Do you need more information to find the cause of this issue?
Hey,



That is an issue with typing-- it looks like `GenericJson` is unable to set a Field, which fails because the expected class doesn't match the actual class. For example, if your `GenericJson` has a `private String userID`, but the value of that is an integer in your backend, JSON deserialization will fail.



All that method does is instantiate a new instance of whatever class is provided, grab every name/value pair within the JSON object, and set it on that new object that was created.



Can you log `commentRefs.get(0)`, and ensure there is a field "_obj", containing a CommentEntity and that the types are what is expected?
Login or Signup to post a comment