0

i tried to typecast a list data to string but its showing error as java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List please help me to solve this issue.this is done using hibernate.

for(int i2=1; i2<=gantttablecolnameList.size(); ++i2)
{
   columnname= (String) ((List)gantttablecolnameList.get(i1)).get(0);
}

this list contains tables column name.

3
  • 2
    java.lang.String cannot be cast to java.util.List That is true . What are you trying to achieve ? Commented Jun 21, 2013 at 8:51
  • You shouldn't start with Hibernate until you have a sound understanding of Java data types. How is gantttablecolnameList declared? And are you sure your loop starts at 1? (List indexes count from 0) Commented Jun 21, 2013 at 8:56
  • gantttablecolnameList is declared as List gantttablecolnameList=new ArrayList(); Commented Jun 21, 2013 at 9:12

3 Answers 3

1

If your List gantttablecolnameList really have String then try this :

for(int i2=0; i2<gantttablecolnameList.size(); i2++)
{
  columnname= (String) (gantttablecolnameList.get(i2));
}

You are doing this :

(List)gantttablecolnameList.get(i1)

Assuming your List gantttablecolnameList contains String ,gantttablecolnameList.get(i1) will give you an Object which is actually a String instance for the index specified by the runtime value of i1 . So , in a nutshell you are trying this :

 (List)"someString";

which is impossible.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

 columnname= (String) ((List)gantttablecolnameList).get(0));

in (List)gantttablecolnameList.get(i1)) the gantttablecolnameList.get(i1) will give you the element ..not the list.

But really you have to check null conditions.Yours is as of now NUll PATTERN

Comments

0

Several things:

  1. Downcast to List <String> instead of the raw type you have now;
  2. Iterate over the list using the enhanced for loop. Don't do index-based iteration;
  3. Once you have dealt with the above, your code will be simple enough to make the bug simply evaporate:

     for (String columname : (List<String>) ganttablecolumnlist) ...
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.