Typecasing java.lang.Object???

T

Tanveer

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

Lee Weiner

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.

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
 
R

Robert Klemme

Tanveer said:
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...
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
 
E

Eric Sosman

Tanveer said:
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...

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

Tanveer

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:eek:rg.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....
 
T

Tanveer

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

Lee Weiner

"Tanveer" said:
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
 
T

Tanveer

Lee said:
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

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
 
P

Patricia Shanahan

Tanveer said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top