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
Solved

Java error ClassCastException

Posted on 2013-05-14
14
435 Views
Last Modified: 2013-05-15
Hello there,

what is wrong with this code. I get this error even though I cast it to Long or am I doing something wrong

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long

Users users = new Users();
users.setId((Long)dsRequest.getFieldValue("id"));

Open in new window


cheers
Zolf
0
Comment
Question by:zolf
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39163879
Please post the class of which 'dsRequest' is an instance
0
 

Author Comment

by:zolf
ID: 39163888
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39163921
Looks pretty straightforward to me - getFieldValue() returns a String which you are trying to cast to Long, which can't happen.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 86

Expert Comment

by:CEHJ
ID: 39164039
MIght be just me but i don't see a method of that name anywhere
0
 
LVL 8

Expert Comment

by:stmani2005
ID: 39164095
i agree with cehj

getfieldvalue method not found in dsrequest class
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39164104
0
 

Author Comment

by:zolf
ID: 39164109
java.lang.Object getFieldValue(java.lang.Object fieldName)
           Returns the value for a particular fieldName.
0
 

Author Comment

by:zolf
ID: 39164111
Krakatoa

it returns an Object and takes an Object which I am passing a String and trying to cast it to long
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 166 total points
ID: 39164116
But not one that can be cast to Long evidently.

Check this method perhaps: getClientSuppliedCriteria
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39164130
java.lang.Object getFieldValue(java.lang.Object fieldName)
           Returns the value for a particular fieldName.
And the link to that method in the supplied javadoc is ..?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 167 total points
ID: 39164133
users.setId(Long.valueOf(dsRequest.getFieldValue("id").toString()));

Open in new window

could be tried but is almost certainly unnecessary
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39164140
Show us the import statements from your code.
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 167 total points
ID: 39165159
this is what you have to write:
users.setId(Long.valueOf( (String) dsRequest.getFieldValue("id")));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39167308
:)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

789 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