简体   繁体   中英

Caused by:java.lang.ClassCastException:

Today I was working on my program and I got this error.

Caused by:java.lang.ClassCastException: org.cubeville.blocks.CrossedBlockBrush cannot be  cast to org.cubeville.blocks.DefaultBlockBrush

My program is in Java of course. It uses lwjgl and slick. I was wondering what might cause this and if there is a for sure fix.

Thank You

First check that DefaultBlockBrush is indeed a supertype of CrossedBlockBrush . If it's not, then the cast is simply illegal. Check whether you're using the same version of your library for both compiling and running; perhaps this relationship holds for one but not the other.

If DefaultBlockBrush is indeed a supertype of CrossedBlockBrush , then you the only way you could get this kind of message would be if CrossedBlockBrush and DefaultBlockBrush were loaded by two different class loaders. This kind of thing can happen in a badly configured web application, or an application based on some other component-based system with multiple ClassLoader s like OSGi.

You are casting something like

DefaultBlockBrush blockBrush = (DefaultBlockBrush) brush;

but brush, on the right hand side is of type CrossedBlockBrush. You could do that if CrossedBlockBrush was a subclass of DefaultBlockBrush, but apparently it is not.

I can tell you that:-

org.cubeville.blocks.CrossedBlockBrush

cannot be cast to a:-

org.cubeville.blocks.DefaultBlockBrush

But that is just saying what the exception said.

Somewhere in your code you have:-

instance = (DefaultBlockBrush) originalInstance

and because CrossedBlockBrush doesn't implement or extend a DefaultBlockBrush you are getting a cast exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM