Solved

ClassCastException

Posted on 2011-03-03
9
329 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
Comment Utility

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
Comment Utility
Y ur casting that in to string ?
0
 

Author Comment

by:cofactor
Comment Utility

>>>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
 

Author Comment

by:cofactor
Comment Utility
>>>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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 20

Expert Comment

by:Sathish David Kumar N
Comment Utility
try with toString() method
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now