Solved

ClassCastException

Posted on 2011-03-03
9
331 Views
Last Modified: 2012-05-11
I'm trying to send multiple textarea values from jsp to be inserted into database. However the textarea values are not received in the Action class properly. I get ClassCastException when trying to insert the data into database.

Whats the workaround ?

Please see the file attached for details
ClassCastException.txt
0
Comment
Question by:cofactor
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35034059

It looks like you are trying to cast array of Strings into String. Try casting to Stirng[].
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35034068
Y ur casting that in to string ?
0
 

Author Comment

by:cofactor
ID: 35034082

>>>Y ur casting that in to string ?

If I dont do , then I get compile error
in java.sql.PreparedStatement cannot be applied to (int,java.lang.Object)
    [javac]                             ps.setString(3,cvo.getItemDescription().get(k));
0
DevOps Toolchain Recommendations

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

 

Author Comment

by:cofactor
ID: 35034096
>>>It looks like you are trying to cast array of Strings into String. Try casting to Stirng[].

please see the prints I put in the attached text file.

My objective is,  I  want to get the String values entered in the textarea and  insert  into database. What would be  the correct code ?
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35034146
try with toString() method
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35034163
try changing your bean to


private List<String> itemDescription;

public List<String> getItemDescription() {
      return itemDescription;
}
public void setItemDescription(List<String> itemDescription) {
      this.itemDescription = itemDescription;
}
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35034215
Where you call this:

(String)cvo.getItemDescription()

Open in new window


You should actually call:
((String[])cvo.getItemDescription())[i]

Open in new window


where i is the index of your text item.
0
 

Author Comment

by:cofactor
ID: 35034337
object is right .  That works . Awesome!

I just put the generic as object suggested  and the error vanished....excellent.

But why the explicit casting did not work ?  Why the generic worked but explicit cast failed ?

I'm using jdk 1.6 .
0
 
LVL 92

Expert Comment

by:objects
ID: 35034359
> But why the explicit casting did not work ?  Why the generic worked but explicit cast failed ?

because it wasn't a list of strings.
Without the generics it cannot accurately determine what the type of object in the list is
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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

772 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