Solved

ClassCastException

Posted on 2011-03-03
9
333 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 78
Java basic valueOf question 1 34
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 65
jmeter usage 4 38
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
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.

726 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