Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 05:05 PM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 05:07 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
Hi,
To do the casting u have to be sure that the obj is really an Integer Object, otherwise u cant do the casting. I think that your _secondVector contains only string objects, so u cant do the sating from object to Integer, try to do the casting from object to string..Bye.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cast an Object into a specific type (Integer/String) at runtime mailtogagan@gmail.com Advanced Java 2 12-03-2007 02:04 PM
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String) baltimore New To Java 1 08-06-2007 08:45 AM
Error: cannot be applied to (java.lang.String) carl New To Java 1 08-05-2007 07:33 AM
Error: java.lang.InterruptedException is not caught and does not appear... fernando Advanced Java 1 07-31-2007 06:52 AM
Can't convert java.lang.String to int. Albert AWT / Swing 2 07-13-2007 06:05 PM


All times are GMT +3. The time now is 09:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org