• Advertisement
Sign in to follow this  

Urgent Help Needed

This topic is 4825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need urgent help with a probably simple cast exception in Java. I have a class called Pile and LayoutPile extends is. I have an object declared such that Pile x = new LayoutPile(), then it it modified and so on using basic pile things. Later, I try to cast it to a LayoutPile, using something like (LayoutPile)x.somefunctiononlyinLayoutPile(), and it runs but crashes because of some unexplained cast exception. What's the deal with this? I see no problem with my code. Please help.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by gamechampionx
(LayoutPile)x.somefunctiononlyinLayoutPile()
This casts the result of calling that function, thus the crash. Try this instead:

((LayoutPile)x).someFunctionOnlyInLayoutPile();

Share this post


Link to post
Share on other sites
I have try that, and it still crashes. It is clearly compatible with the type since it's made a new LayoutPile when initialized but then I cast it and it just crashes. Could my compiler be making a mistake cause nobody I talk to can figure out why this is happening.

Share this post


Link to post
Share on other sites
I was going to say what Oluseyi said, but seeing as you tried that, I would like to see the code within the function. Perhaps you can print some debug info, to see if the virtual machine actually makes the jump into the function, or breaks before it runs. My guess is something is happening in the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by gamechampionx
I have try that, and it still crashes.
Then it's not the cast. Your error lies elsewhere, and nobody can figure it out because they don't have enough information to do so.

Time to pony up the [relevant] source, buddy.

Share this post


Link to post
Share on other sites
Are you even allowed to cast a into a b when b is declared such that a varname = new b, where the class b extends the class a, or can you only do this when you implement something?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement