Solved

ClassCastException

Posted on 2011-03-03
9
332 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Java string replace 11 54
How to convert from xls to xlsx using java 7 55
Java program running SQL query 5 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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