Solved

cannot cast Integer to Long

Posted on 2014-04-20
2
752 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video teaches viewers about errors in exception handling.

735 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