Skizit
Skizit

Reputation: 44862

Play-Framework: Causing a 'IllegalArgumentException occured

The error I get is 'IllegalArgumentException occured' Can not set java.util.ArrayList field mi.types.ListOfObjects.objects to java.util.LinkedList

I'm doing the following...

 ListOfObjects objects = li.getUsersObjects();

which works perfectly fine normally, however when I do the exact same call with the exact same code inside of Play it doesn't. I'm calling it in my Security Controller inside the authenticate() function. There's a case where the users objects are gotten from an external server. That's when this call is made. It works fine without Play and I'm pretty sure worked before I put it in the Security Controller. Why would putting it here cause such a problem?

EDIT

ListOfObjects is a custom object which contains some values.. It's located inside li which is a library I'm importing. It looks something like this...

public class ListOfObjects {

  private ArrayList<Object> objects;

  public ArrayList<Object> getObjects(){
          return objects;
   }
}

getUserObjects simply returns the objects for that user. It grabs them from a server and then uses gson to parse them. However I'm getting the above error when I attempt to.

EDIT2

No matter what I do my type for getUserObjects seems to always be returning as a LinkedList even though it's an ArrayList object. I've tried calling getObjects() directly and there's no change.

EDIT 3

getUserObjects is defined as...

        Gson gson = new Gson();
    ListOfObjects objects = gson.fromJson(r.getBody(), ListOfObjects.class);

StackTrace...

where r.getBody() is the JSON response from the server

   Execution exception (In /app/controllers/Security.java around line 57)
 IllegalArgumentException occured : Can not set java.util.ArrayList field     lm.types.ListOfObjects.objects to java.util.LinkedList

play.exceptions.JavaExecutionException: Can not set java.util.ArrayList field lm.types.ListOfObjects.objects to java.util.LinkedList
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
at Invocation.HTTP Request(Play!)
 Caused by: java.lang.IllegalArgumentException: Can not set java.util.ArrayList field lm.types.ListOfObjects.objects to java.util.LinkedList
at com.google.gson.FieldAttributes.set(FieldAttributes.java:188)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:118)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
at com.google.gson.Gson.fromJson(Gson.java:406)
at lm.lib.LMethods.getUsersObjects(LMethods.java:165)
at controllers.Security.createNewUser(Security.java:57)
at controllers.Security.authenticate(Security.java:36)
at play.utils.Java.invokeStaticOrParent(Java.java:159)
at controllers.Secure$Security.invoke(Secure.java:193)
at controllers.Secure$Security.access$0(Secure.java:184)
at controllers.Secure.authenticate(Secure.java:64)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
... 1 more

Debugger failed to attach: recv failed during handshake: Connection reset by peer

Upvotes: 1

Views: 1427

Answers (1)

axtavt
axtavt

Reputation: 242786

Exception message clearly says that Gson deserializes list as LinkedList, whereas field of your ListOfObjects is declared as ArrayList.

The general rule to avoid this kind of problems is not to use implementation classes in field declarations. Use List instead:

public class ListOfObjects {
    private List<Object> objects; 
    ...
}

Upvotes: 3

Related Questions