View Single Post
  #1 (permalink)  
Old 08-01-2007, 06:05 PM
barney barney is offline
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
Cast Error Caught (change) Class is really: java.lang.String
Hi, I was getting a class cast exception in part of my code (using Java 2 sdk 1.4.1). I added an exception handler and the following is the section of the code:

Code:
Object obj = _secondVector.get(i); try{ Integer y = (Integer)obj; _firstVector.set(y.intValue(), "<<<remove>>>"); }catch(ClassCastException e){ System.err.println("Cast Error Caught (change)"); System.err.println("Class is really: " + obj.getClass().getName()); e.printStackTrace(); }
_secondVector is simply of the Vector class. this, i thought returned an Object. However, the exception handler prints out:

Code:
Cast Error Caught (change) Class is really: java.lang.String
can anyone shed some light on why obj is a String instead of an Object?

i tried changing the line:

Code:
Integer y = (Integer)obj;
to:
Code:
Integer y = new Integer(obj);
Because it said that obj was actually a String and there is an Integer constructor with String as the argument, but i get:
Code:
cannot resolve symbol symbol : constructor Integer (java.lang.Object) location: class java.lang.Integer Integer y = new Integer(obj);
So now it's telling me that obj is an object, and before that it was a string......
Thanks.
Reply With Quote
Sponsored Links