Java error ClassCastException

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
zolfAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
users.setId(Long.valueOf(dsRequest.getFieldValue("id").toString()));

Open in new window

could be tried but is almost certainly unnecessary
0
 
CEHJCommented:
Please post the class of which 'dsRequest' is an instance
0
 
zolfAuthor Commented:
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
krakatoaCommented:
Looks pretty straightforward to me - getFieldValue() returns a String which you are trying to cast to Long, which can't happen.
0
 
CEHJCommented:
MIght be just me but i don't see a method of that name anywhere
0
 
Manikandan ThiagarajanSenior consultantCommented:
i agree with cehj

getfieldvalue method not found in dsrequest class
0
 
zolfAuthor Commented:
java.lang.Object getFieldValue(java.lang.Object fieldName)
           Returns the value for a particular fieldName.
0
 
zolfAuthor Commented:
Krakatoa

it returns an Object and takes an Object which I am passing a String and trying to cast it to long
0
 
krakatoaConnect With a Mentor Commented:
But not one that can be cast to Long evidently.

Check this method perhaps: getClientSuppliedCriteria
0
 
CEHJCommented:
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
 
krakatoaCommented:
Show us the import statements from your code.
0
 
ValeriConnect With a Mentor Commented:
this is what you have to write:
users.setId(Long.valueOf( (String) dsRequest.getFieldValue("id")));
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.