0

I'm trying to access an object in a LinkedHashMap, but I get an InvocationTargetException when I try to do it. The LinkedHashMap is the result of a conversion from JSON to a series of Java objects using the Gson library. The object 'List' contains every other element:

public class List{
   private Clouds clouds;
   private Number dt;
   private String dt_txt;
   private Main main;
   private Rain rain;
   private Sys sys;
   private List weather;
   private Wind wind;
}

The class also contains getters and setter.

When I do the following:

for(LinkedHashMap l : ArrayList<LinkedHashMap> result.getList()) {
    Number dt = (Number) l.get("dt");
}

I can access the variable dt, and it actually returns a value. However when I try to access any other property like the Main property like this:

Main main = (Main) l.get("main");

I'll get an InvocationTargetException. Any thoughts, tips or tricks?

Edit:

Stacktrace:

 09-19 14:37:59.448: E/AndroidRuntime(11192): FATAL EXCEPTION: main
 09-19 14:37:59.448: E/AndroidRuntime(11192): java.lang.ClassCastException:    com.google.gson.internal.LinkedHashTreeMap cannot be cast to com.censored.weather.Main
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at   com.censored.censored.VenloAppDelegate$GetWeatherTask.onPostExecute(VenloAppDelegate.java:305)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at com.censored.censored.VenloAppDelegate$GetWeatherTask.onPostExecute(VenloAppDelegate.java:1)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.os.AsyncTask.finish(AsyncTask.java:631)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.os.AsyncTask.access$600(AsyncTask.java:177)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.os.Handler.dispatchMessage(Handler.java:99)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.os.Looper.loop(Looper.java:153)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at android.app.ActivityThread.main(ActivityThread.java:5297)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at java.lang.reflect.Method.invokeNative(Native Method)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at java.lang.reflect.Method.invoke(Method.java:511)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
 09-19 14:37:59.448: E/AndroidRuntime(11192):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
giampaolo
  • 6,906
  • 5
  • 45
  • 73
Tim Kranen
  • 4,202
  • 4
  • 26
  • 49
  • 2
    First, post the entire stack trace. Second, `List` is a very dangerous choice for a class name in Java. Third, I'm skeptical that your `for` loop is actual code as opposed to an example you came up with for demonstration; what's with the declaration syntax? – chrylis -cautiouslyoptimistic- Sep 19 '13 at 12:30
  • Yeah the for loop is just a small demonstrative piece of code because the actual source code is rather long and complicated. I know List is a very dangerous choice, but I didn't choose it. This is an existing project which is rather large. I'll post the stack trace but I debugged it and throws an InvocationTargetException when I'm trying to assign l.get('main') to main. – Tim Kranen Sep 19 '13 at 12:36
  • 1
    Okay, but from where? I doubt the `get` call is throwing the exception, and the answer to your question is probably contained in what the under-the-hood reflection system is trying to do. Part of the reason that contrived examples aren't helpful is that we can't see what actual types are involved to understand how `get` is being implemented. – chrylis -cautiouslyoptimistic- Sep 19 '13 at 12:38
  • Well, I wasn't clear before, it's actually a ClassCastException at the line where the get is. So it's almost definitely wrong there. – Tim Kranen Sep 19 '13 at 12:42
  • 1
    Well, there you go; `l.get("main")` is returning a `LinkedHashTreeMap` instead of whatever you were expecting; I strongly suspect that `Main` is a nested complex type, and you're viewing it as a map instead of as a POJO. The exception you're getting from that line is a `ClassCastException`, not an `InvocationTargetException`. – chrylis -cautiouslyoptimistic- Sep 19 '13 at 12:43
  • Thanks! Is there anyway to retrieve the POJO main object from that LinkedHashTreeMap? – Tim Kranen Sep 19 '13 at 12:46
  • I've never used Gson; I'm just telling you what the stack trace is telling me the problem is. I presume there's some way to tell it to map the JSON back onto POJOs, but you're mixing approaches here; either convert the whole thing into a POJO graph, or use `Map#get` all the way through. – chrylis -cautiouslyoptimistic- Sep 19 '13 at 12:49
  • To help you, I would be nice to have the JSON string you are starting from. – giampaolo Sep 20 '13 at 21:07

0 Answers0