Solved

cannot cast Integer to Long

Posted on 2014-04-20
2
747 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
2 Comments
 
LVL 26

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

776 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