Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Typecasing java.lang.Object???

Thread view: 
Tanveer - 24 Sep 2006 18:57 GMT
One of my function returns java.lang.Object type of object

My problem is:

There is a method called getData() as:

java.lang.Object getData();

but i want to typecast it as:  PDataNode

I want to do something like:

PDataNode node1 = temp.getData();

But this is not possible, because , PDataNode is not of type
java.lang.Object, so i did

PDataNode node1 = (PDataNode) temp.getData();

Isn't this allowed in Java.
I thought , concepts like typecasting will surely work in java.
Though it gets compiled but gives runtime error.

I am beginner in Java, so please consider my basic question as well...
Lee Weiner - 24 Sep 2006 19:16 GMT
>One of my function returns java.lang.Object type of object
>
[quoted text clipped - 18 lines]
>I thought , concepts like typecasting will surely work in java.
>Though it gets compiled but gives runtime error.

There is no getData() method in the Object class, so you must be talking about
a class that extends Object.  What type is temp (the class that contains the
getData() method)?

Lee Weiner
lee AT leeweiner DOT org
Tanveer - 24 Sep 2006 19:34 GMT
getData is user defined method in my class, let;s say "MyClass" .
This class : MyClass is provided as part of library, given with a tool.
I don't know much about this apart from the fact that, it says..

java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl
    at com.mentor.pxSample.PdGenerator.dump(PdGenerator.java:1006)
    at com.mentor.pxSample.PdGenerator.generate(PdGenerator.java:1233)
    at
com.mentor.px.generator.chain.PxGeneratorWrapper.runGenerator(PxGeneratorWrapper.java:72)
    at
com.mentor.px.generator.chain.PxGeneratorWrapper.generate(PxGeneratorWrapper.java:73)
    at
com.mentor.px.generator.chain.PxGeneratorChain.generate(PxGeneratorChain.java:141)
    at
com.mentor.px.generator.chain.PxGeneratorChain.startGeneratorChain(PxGeneratorChain.java:48)
    at
com.mentor.PlatformExpressImpl.plugin.editor.dynamicMenus.RunGenerator.executeGenerator(Unknown
Source)
    at
com.mentor.PlatformExpressImpl.plugin.editor.dynamicMenus.RunGenerator$1.run(Unknown
Source)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:76)
java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl
    at com.mentor.pxSample.PdGenerator.dump(PdGenerator.java:1006)
    at com.mentor.pxSample.PdGenerator.generate(PdGenerator.java:1233)
    at
com.mentor.px.generator.chain.PxGeneratorWrapper.runGenerator(PxGeneratorWrapper.java:72)
    at
com.mentor.px.generator.chain.PxGeneratorWrapper.generate(PxGeneratorWrapper.java:73)
    at
com.mentor.px.generator.chain.PxGeneratorChain.generate(PxGeneratorChain.java:141)
    at
com.mentor.px.generator.chain.PxGeneratorChain.startGeneratorChain(PxGeneratorChain.java:48)
    at
com.mentor.PlatformExpressImpl.plugin.editor.dynamicMenus.RunGenerator.executeGenerator(Unknown
Source)
    at
com.mentor.PlatformExpressImpl.plugin.editor.dynamicMenus.RunGenerator$1.run(Unknown
Source)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:76)
[ERROR]   - Generator 'com.mentor.pxSample.PdGenerator' Had the
following error:
java.lang.ClassCastException:org.apache.xerces.dom.ElementNSImpl

I don't how much these messages make sense.  PdGenerator.java is the
java file i am using.

It has typeconversion code as given earlier, dump is the function in
which this typecasting is done....
Tanveer - 24 Sep 2006 19:38 GMT
Thanks Eric, for the best explanation i would have ever got on
classExceptionCast.

But actually, my problem is how do i do that???, i agree that it is not
possible, but still i want to do it....
Lee Weiner - 24 Sep 2006 21:23 GMT
>Thanks Eric, for the best explanation i would have ever got on
>classExceptionCast.
>
>But actually, my problem is how do i do that???, i agree that it is not
>possible, but still i want to do it....

What kind of object is getData() returning?  If, for example, it's returning a
String that contains the data in a PDataNode object, you can retrieve the data
as a String, parse it into fields and construct a PDataNode object.

Lee Weiner
lee AT leeweiner DOT org
Tanveer - 25 Sep 2006 06:01 GMT
> >Thanks Eric, for the best explanation i would have ever got on
> >classExceptionCast.
[quoted text clipped - 8 lines]
> Lee Weiner
> lee AT leeweiner DOT org

It is returning an object of type java.lang.Object.

But simple typecast to PxDataNode, does not solve the purpose.

How is java.lang.Object different and what is it's use in my
context...??? That will surely help
Patricia Shanahan - 25 Sep 2006 07:26 GMT
>>> Thanks Eric, for the best explanation i would have ever got on
>>> classExceptionCast.
[quoted text clipped - 14 lines]
> How is java.lang.Object different and what is it's use in my
> context...??? That will surely help

I think you may be confused about a pair of very important concepts in
java, class and type.

Each object has a class that is determined when it is created, and
cannot be changed during its lifetime.

During the object's lifetime, there will be reference expressions,
including reference variables, that point to it. Each reference
expression has a type that is fixed at compile time, regardless of which
object it references.

However, Java ensures that the type of a reference expression that
points to a given object always corresponds to the object's class,
or one of its superclasses, or an interface its class implements,
directly or indirectly.

For example, the object "xxx" is of class String. It can be referenced
by expressions of types String, Object, Serializable, CharSequence, or
Comparable.

This ensures that any object pointed to by a reference expression has
all the members associated with the expression's type. If a Comparable
expression points to an object, that object really does have a compareTo
method.

If you want to be able to reference an object as a PxDataNode, you must
create it as a PxDataNode, or an instance of a PxDataNode subclass. If
you are creating it e.g. by "new Object()", it does not have the
features of a PxDataNode and can never be referenced as a PxDataNode.

It might be a good idea to explain at a higher level what you are trying
to achieve.

Patricia
Robert Klemme - 24 Sep 2006 19:24 GMT
> One of my function returns java.lang.Object type of object
>
[quoted text clipped - 20 lines]
>
> I am beginner in Java, so please consider my basic question as well...

You probably do not get a PDataNode back.  It would be helpful if you
provide more context (where is that method defined? what exectpt
exception do you get?)

Kind regards

    robert
Eric Sosman - 24 Sep 2006 19:30 GMT
> One of my function returns java.lang.Object type of object
>
[quoted text clipped - 20 lines]
>
> I am beginner in Java, so please consider my basic question as well...

    Hint: Instead of saying "runtime error" and making us all
guess which of the many possible errors it might have been,
consider showing us the actual error message.  If you were a
doctor, would you prefer to hear a patient complain "Doc, I
feel bad" or "Doc, I've been vomiting since midnight and I'm
running a fever and my vision is blurry?"  Give the diagnostician
all the information you can.

    Since you force me to guess, I'll guess that "runtime error"
was either a NullPointerException or a ClassCastException -- if
it was actually something else, you have only yourself to blame
for concealing its nature.

    If the error was NullPointerException, it means that `temp'
had the value `null' at the moment when you tried to call its
getData() method.  That is, `temp' was a variable capable of
referring to an object (of a type that has a getData() method),
but at the time you tried the call it was not actually referring
to anything at all.  You can ask someone on the street "Excuse
me, what time is it?" but if there's nobody there and you ask
the question of thin air you can't expect anything good to happen.

    If the error was ClassCastException, it means that getData()
returned a reference to an Object that didn't happen to be a
PDataNode object.  It may have returned a reference to a String
or to an Integer or to a YourHonorIObject, but it didn't return
a reference to a PDataNode.  You cannot (as in C) simply tell
the system to pretend that an object is of some unrelated type;
casts are checked for validity at run-time, and "punning" casts
are disallowed.  When you write a cast, you say "Listen, Java,
I know getData() only promises to return an Object, but I happen
to know that the Object is actually a PDataNode."  Java replies
"Trust, but verify" -- and if you lied to it, ClassCastException.

Signature

Eric Sosman
esosman@acm-dot-org.invalid



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.