Question

I am having troubles with casting, and I cannot figure it out. According to my research, what causes a ClassCastException when you cast a superclass to your class, the reason is they both use different Class Loaders.

I did a simple System.out.println() to check if the class loaders are different, but they both print out the same result, @11cb01a.

To get to the point on what I'm trying to accomplish, what I am attempting is creating a simple API for myself to make minigames with (I am using the Bukkit API) and I have found errors at two locations of having a ClassCastException. First off, I have two classes, the ArenaBase and ArenaManagerBase. I have a class called ArenaManager that extends ArenaManagerBase, and Arena that extends ArenaBase.

When I get my error for a ClassCastException for ArenaManager, it's from calling ArenaManager.getInstance() and casting (ArenaManager) to it. When it first starts, I initialize my ArenaManager.

ArenaManager.init(this);

Then, inside of the init I call a few things.

public static void init(JavaPlugin instance) {
    am = new ArenaManagerBase(); // What is returned when calling getInstance()
    am.plugin = instance;
    am.ac = new ArenaConfigBase(am.plugin);
    am.init(); // Separate initializer for any subclass that might need it.
}

The part that throws the error is this:

ArenaManager am = (ArenaManager) ArenaManager.getInstance();

So just from this, I have a class that I am trying to cast to that is a superclass of the other. They both seemingly have the same ClassLoader. What am I doing wrong, and how could I fix it?

Edit:
Here's the stacktrace. It's weird, because at the top it says null and then at the bottom it contemplates a CastClassException.

[18:36:10 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'cast
ledefend' in plugin DefendTheCastle vAlpha
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
Caused by: java.lang.ClassCastException: me.codermusgrove.arenaapi.ArenaManagerB
ase cannot be cast to me.valdeon.defendthecastle.arena.ArenaManager
        at me.valdeon.defendthecastle.cmd.CmdCreateArena.onCommand(CmdCreateAren
a.java:27) ~[?:?]
        at me.valdeon.defendthecastle.cmd.BaseCommand.onCommand(BaseCommand.java
:30) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
Was it helpful?

Solution

An instance of a child class can be referred to using a parent type variable. But you cannot do the reverse. Although, you can bypass the compiler error checking by having an explicit cast like - (ArenaManager) ArenaManager.getInstance(), it will eventually fail at runtime with a CCE.

ArenaManagerBase is the parent and ArenaManager is the child in your case. The instance that you have is of the parent (i.e. new ArenaManagerBase()), and you are trying to cast it the child.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top