Solved

cannot cast Integer to Long

Posted on 2014-04-20
2
761 Views
Last Modified: 2014-04-21
below json data that is coming from the business layer.these values are coming dynamically from db.

when  iterate the json and cast it to Long ,getting classcastexception for value 261031668.

"cannot cast Integer to Long".
{Cluster1=4560722019, Cluster2=8796913840, Cluster3=4562452584, Cluster4=4557194248, Cluster5=261031668}

innerEntry.getValue()...4560722019
innerEntry.getValue()...8796913840
innerEntry.getValue()...4562452584
innerEntry.getValue()...4557194248
innerEntry.getValue()...261031668

Open in new window

when we get this kind of big range values what is the generic type we need to use for innerMap in this case.as you can see i have taken innerMap<String,Long>.
 
       
      
                for(Entry<String, Long> innerEntry : innerMap.entrySet()) {
                     System.out.println("innerEntry.getValue()..."+innerEntry.getValue());

                    Long sizeObj = (Long)innerEntry.getValue(); //here getting classcastexception for value 261031668

Open in new window

0
Comment
Question by:chaitu chaitu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 40011979
If you're going to sometimes receive a Long and sometimes receive an Integer then you can consider using Number:

innerMap<String,Number>

Since Number is a base class for both Integer and Long the assignment will work for both Integer and Longs:

Number sizeObj = (Number)innerEntry.getValue() ;

and then when you wish to extract the values from the map you can use longValue() which will be valid for integers and longs:

public long getLongValue(String key) {
   Number lookupValue = innerMap.get(key) ;
   return lookupValue.longValue() ;
}

Doug
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 40014107
excellent doug.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question