Solved

Java error ClassCastException

Posted on 2013-05-14
14
427 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now