NullPointerException?

Discussion in 'Plugin Development' started by MOMOTHEREAL, Oct 19, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    MOMOTHEREAL

    Hello, I have been coding plugins for a while now.
    This time, I tried organizing my code in different classes instead of having a HUGE Main class.
    For the first time, I get this error:
    Code:
    [SEVERE] Error occurred while enabling FightArena v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.momo.fight.EnableDisable.EnablePlugin(EnableDisable.java:9)
        at me.momo.fight.FightArena.onEnable(FightArena.java:10)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:264)
        at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:315)
        at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:292)
        at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:252)
        at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.java:152)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:393)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Here is my code:

    FightArena class:
    Code:java
    1. package me.momo.fight;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class FightArena extends JavaPlugin{
    6. //Creates a new instance of EnableDisable class\\
    7. EnableDisable endis = new EnableDisable();
    8. public void onEnable(){
    9. //Enables the plugin using the EnableDisable EnablePlugin void\\
    10. endis.EnablePlugin();
    11. }
    12. public void onDisable(){
    13. //Disables the plugin using the EnableDisable DisablePlugin void\\
    14. endis.DisablePlugin();
    15. }
    16.  
    17. }
    18.  

    EnableDisable class:
    Code:java
    1. package me.momo.fight;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class EnableDisable extends JavaPlugin{
    6. Strings strings = new Strings();
    7. public void EnablePlugin() {
    8. //Enables the plugin\\
    9. getLogger().info(strings.ENABLE_MESSAGE);
    10. }
    11. public void DisablePlugin() {
    12. //Disables the plugin\\
    13.  
    14. getLogger().info(strings.DISABLE_MESSAGE);
    15. }
    16. }
    17.  

    And finally, Strings class:
    Code:java
    1. package me.momo.fight;
    2.  
    3. public class Strings {
    4.  
    5. public String ENABLE_MESSAGE = "FightArena has been enabled.";
    6. public String DISABLE_MESSAGE = "FightArena has been disabled.";
    7.  
    8. }
    9.  


    Thanks for reading!
     
  2. Offline

    adam753

    Unfortunately, making classes extend JavaPlugin doesn't magically give them access to the main class's methods. When you do getLogger() in your main class, it works because that class is handled by Bukkit, hence why it needs to extend JavaPlugin. EnableDisable, however, doesn't get given the logger or anything like that, so getLogger() will return null. What you need to do instead is give EnableDisable a reference to the FightArena instance.

    tl;dr: Never extend JavaPlugin except for the main class, it won't work.
     
    Skyost likes this.
Thread Status:
Not open for further replies.

Share This Page