catasturslykid
catasturslykid

Reputation: 75

Cast double to long, weird exception

I'm parsing a JSON file with org.json.simple and I've got an error that is pissing me off and I can't fix it. Hope you can help me. The problem is that, with this code appears the following exception java.lang.Double cannot be cast to java.lang.Long The "weird" thing of it is that, if I change the "double variables" to "long variables", the following error appears: java.lang.Long cannot be cast to java.lang.Double. Can you please help me?

private double[] tminOWaux = new double[7];
private double[] tmaxOWaux = new double[7];
private double[] humOWaux = new double[7];
private double[] windOWaux = new double[7];
public void filtraOW7days(String contenidoOW) throws ParseException{ 
    JSONParser parser = new JSONParser();
    try{
        Object obj = parser.parse(contenidoOW);
        JSONObject jsonList = (JSONObject) obj;
        JSONArray list = (JSONArray) jsonList.get("list");
        Iterator<String> unitsIterator = list.iterator();
        int i = 0;
        while(unitsIterator.hasNext()){
            Object uJson = unitsIterator.next();
            JSONObject uj = (JSONObject) uJson;
            this.humOWaux[i] =  (double) uj.get("humidity");
            this.windOWaux[i] = (double) uj.get("speed");

            JSONObject slideContent = (JSONObject) uj.get("temp");
            this.tmaxOWaux[i] = (double) slideContent.get("max");
            this.tminOWaux[i] = (double) slideContent.get("min");

            i++;
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }

}

The JSON is:

{
"city":{
"id":2643743,
"name":"London",
"coord":{
"lon":-0.12574,
"lat":51.50853
},
"country":"GB",
"population":0
},
"cod":"200",
"message":0.0217,
"cnt":7,
"list":[
{
"dt":1440590400,
"temp":{
"day":18.89,
"min":15.64,
"max":18.89,
"night":15.64,
"eve":18.89,
"morn":18.89
},
"pressure":1008.2,
"humidity":97,
"weather":[ 
{
"id":501,
"main":"Rain",
"description":"moderate rain",
"icon":"10d"
}
],
"speed":4.37,
"deg":229,
"clouds":76,
"rain":5.8
},
]
}

Thank you for your time!

Upvotes: 1

Views: 1315

Answers (2)

PNS
PNS

Reputation: 19905

The JSONObject is actually a HashMap that may have values of different types.

Apparently, the 4 calls to get() for the array assignments in the while loop may return a Number object (either a Long or a Double) and casting among these types is not allowed in Java.

So, what is needed is a simple helper function like either of

  private static long longValue(Object value) {
    return (value instanceof Number ? ((Number)value).longValue() : -1);
  }

  private static double doubleValue(Object value) {
    return (value instanceof Number ? ((Number)value).doubleValue() : -1.0);
  }

where -1 denotes an invalid value (which of course can be anything else). Using instanceof should be fast enough and it also covers for null values, thus avoiding NullPointerExceptions.

If all the array values to be assigned are long, then the latter 3 assignments need to call longValue(). If all the array values are double, using doubleValue() for the first assignment should work, i.e.

this.humOWaux[i] = doubleValue(uj.get("humidity"));

For safety reasons, though, better use the longValue() or doubleValue() for all assignments, as applicable.

Upvotes: 3

Mauren
Mauren

Reputation: 1975

I suppose your problem lies in the fact that you can't convert wrappers directly from one type to another. This kind of conversion is only allowed with primitives, as you can see in the Java SE Specifications, Chapter 5. Conversions and Promotions.

I'd try using Double.longValue() instead of casting directly.

Upvotes: 1

Related Questions