Weird error's

Discussion in 'Spigot Plugin Development' started by coolnickname, May 6, 2015.

  1. Hello, I made a new plugin, but when I startup my server I get this error:
    Code (Text):
    [09:59:32] [Server thread/ERROR]: Could not load 'plugins\BattleCraft.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:336) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:258) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.java:369) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:152) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
    Caused by: java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_25]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        ... 6 more
    Caused by: java.lang.NullPointerException
        at nl.yildri.BattleCraft.Main.<clinit>(Main.java:22) ~[?:?]
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_25]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        ... 6 more
    And on reload:
    Code (Text):
    [09:56:48] [Server thread/ERROR]: Error occurred while enabling battleCraft v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at nl.yildri.BattleCraft.Main.<init>(Main.java:17) ~[?:?]
        at nl.yildri.BattleCraft.PlayerJoinListener.<init>(PlayerJoinListener.java:10) ~[?:?]
        at nl.yildri.BattleCraft.Main.onEnable(Main.java:43) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:412) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:866) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(CraftServer.java:753) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:326) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:290) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at nl.yildri.BattleCraft.Main.<init>(Main.java:17) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_25]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:336) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:258) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.java:369) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:864) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        ... 10 more
    I have no idea what causes this error's. any help would be appreciated.
     
  2. You are dereferencing a null pointer somewhere in your main class.
     
  3. @Rocoty
    You mean something like this:
    Code (Text):
    public static Score score;
    Or this:
    Code (Text):
    public static Plugin getPlugin() {
            return plugin;
        }
    ???
     
  4. That declares a static field whose value is initially null, which could help explain the exception, but not by itself. Look, it's pointless to ask for help and post single lines of code. Post your main class and we can more easily help you.
     
  5. http://pastebin.com/z1XPKmzW
     
  6. On line 21 you are getting the Bukkit scoreboard manager. This line is called during the main class static initialisation, which is way before the scoreboard manager is initialised. Therefore you get null, and any subsequent dereferencing done on this field will throw a NullPointerException.

    I also notice that you are setting the plugin field to null in onEnable and this in onDisable. That's not gonna do you much good.
     
  7. @coolnickname remove all the static, move all the initialisation to onEnable. Optimally, scoreboard stuff should be initialised a tick later (using a delayed task, see this)
     
  8. Oh, that explains something, but where should I put the scorebaord stuff? because if i put them in onEnable I cant call anymore.
    and yeah that last part wasn't so smart. Thanks for noticing!
     
  9. I did it, but now I get errors in another class, because I'm not able to call them anymore:
    http://pastebin.com/gKbaQzGT

    BTW: to put it into onEnable(), I also had to remove the public.
     
  10. okay, fixed the biggest part myself, the plugin loads, but right AFTER it is enabled, it gives the error it before gave when I reloaded. But it does that after it say's it has been enabled. the error:
    Code (Text):
    [11:52:23] [Server thread/INFO]: [battleCraft] Enabling battleCraft v1.0
    [11:52:23] [Server thread/INFO]: battleCraft Version 1.0 Has Been Enabled!
    [11:52:23] [Server thread/ERROR]: Error occurred while enabling battleCraft v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at nl.yildri.BattleCraft.Main.<init>(Main.java:17) ~[?:?]
        at nl.yildri.BattleCraft.PlayerJoinListener.<init>(PlayerJoinListener.java:11) ~[?:?]
        at nl.yildri.BattleCraft.Main.onEnable(Main.java:42) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:412) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at nl.yildri.BattleCraft.Main.<init>(Main.java:17) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_25]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:336) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:258) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.java:369) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:152) ~[spigot1649.jar:git-Spigot-1.7.9-R0.2-205-g0a049fa]
        ... 2 more
    the new main class:
    http://pastebin.com/ki8zky8U
     
  11. @coolnickname in PlayerJoinListener, you use
    Code (Java):
    new Main();
    Simply put: don't. Pass the plugin instance ('this' in the class itself - so in the Main class, 'this' refers to the plugin instance) through the constructor to the player listener.
     
  12. Already fixed it:D I let al my classes extend the Main class, wich extended JavaPlugin, and for some reason, the makes the plugin load twice:D
     
  13. Why not simple? Create getInstance() method.

    Variable: static (mainclassname) plugin;

    Enable: plugin = this;
    Disable: plugin = null;
    getInstance() method:
    public static Main getInstance(){
    return plugin;
    }
     
  14. Totally different topic, but do you know how I can easy get variables form another class? I tried everything!
     
  15. uh, make them static if you have to, but make sure youre right. Then call them with (classname).(variable)
     
  16. Getters and setters.
     
  17. Because you are relying on bad habits. Singletons are not there for easy access.
     
  18. Why is this a bad habit? I think it is just good code. How would you do it ?
     
    • It violates the open/closed principle (not allowing you to extend the class)
    • It tightly couples classes where not necessary.
    • It violates the encapsulation principle (which is a core principle of object oriented programming)
    • It's not (unit) testable.
    • It introduces a global state in your plugin (making it harder to reason about).
    • It easily introduces flaws and errors.
    Dependency injection, pass the instance as param in the constructor of the class which needs it.
     
  19. So u mean this:

    Var: (mainclass) plugin;

    Constructor:

    public (classname)(Main instance){
    plugin = instance
    }

    When u need to call main class :
    plugin.(in the mainclass)

    ?