?
Solved

cannot cast Integer to Long

Posted on 2014-04-20
2
Medium Priority
?
771 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 28

Accepted Solution

by:
dpearson earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
A solution for Fortify Path Manipulation.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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