nullpointerexception for getConfig

Discussion in 'Plugin Development' started by KiF, Jun 28, 2014.

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

    KiF

    I've been trying to use my plugin's config file to store some data, but when I run it, I get this error:
    Line 265:
    thing is an instance of my main class.
     
  2. Offline

    Plo124

    KiF
    You need to initiallise the config in your onEnable

    this.getConfig().options().copyDefaults(true);
     
  3. Offline

    KiF

    Plo124 I tried initializing the config in my onEnable with that line, but it didn't appear to do anything.
     
  4. Offline

    fireblast709

    Plo124 no need to call copyDefaults()
    KiF thing is null
     
    KiF likes this.
  5. Offline

    KiF

    fireblast709 I initialized thing now, but I get more problems after doing that:
    StackTrace (bottom to top):
    Code:
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:126) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.DedicatedPlayerList.<init>(SourceFile:14) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.PlayerList.<init>(PlayerList.java:68) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.craftbukkit.v1_7_R3.CraftServer.<init>(CraftServer.java:319) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugins(CraftServer.java:357) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at java.lang.Class.newInstance(Class.java:433) ~[?:1.8.0_05]
    28.06 18:52:46 [Server] INFO at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[?:1.8.0_05]
    28.06 18:52:46 [Server] INFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_05]
    28.06 18:52:46 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_05]
    28.06 18:52:46 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_05]
    28.06 18:52:46 [Server] INFO at me.KiF.ArenaBattle.thing1.<init>(thing1.java:13) ~[?:?]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO Caused by: java.lang.IllegalStateException: Initial initialization
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:382) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:448) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at me.KiF.ArenaBattle.thing1.onEnable(thing1.java:25) ~[?:?]
    28.06 18:52:46 [Server] INFO at me.KiF.ArenaBattle.ABListener.<init>(ABListener.java:19) ~[?:?]
    28.06 18:52:46 [Server] INFO at me.KiF.ArenaBattle.thing1.<init>(thing1.java:13) ~[?:?]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit_1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    28.06 18:52:46 [Server] INFO java.lang.IllegalArgumentException: Plugin already initialized!
    28.06 18:52:46 [Server] ERROR Error occurred while enabling ArenaBattlePlugin v1.0.3 (Is it up to date?)
    Line 13:
    Line 19:
    Line 25:
    All suggestions are appreciated.
     
  6. Offline

    fireblast709

    KiF never ever create an instance of your main class. Pass it to other classes using the constructor
     
  7. Offline

    KiF

    I've now tried using

    instead of the previous way I tried it, but now I'm back where I started, with the nullpointerexception at the same place. Could you provide an example pls?
     
  8. Offline

    Gater12

  9. Offline

    iiHeroo

    Check if the player is null.....
     
  10. Offline

    KiF

    Gater12 I tried what you said, but that makes the method invalid since it's an eventhandler method.
    iiHeroo player is not null
     
  11. Offline

    Gater12

    KiF
    Have you even taken a look at the site I gave you? It gives you a detailed explanation of constructors.
     
  12. Offline

    BlazingBroGamer

    KiF
    I think this way is easier...
    Code:
    //In your main class
    public static [Class Name] instance;
     
    public void onEnable(){
    instance = this;
    }
     
    public void onDisable(){
    instance = null;
    }
     
    public static [Class Name] getInstance(){
    return instance;
    }
    
    And in your other class where you want the instance,
    Code:
    [Main Class Name] main = [Main Class Name].getInstance();
    
    Thats how I did it
     
  13. Offline

    Gater12

    BlazingBroGamer
    You really shouldn't have a static instance of your main class. You should use constructors to pass the instance, and it shows that you know the concepts of OOP.

    But if you really wanted a static instance of your plugin:
    Code:java
    1. public static Main getInstance(){
    2. return JavaPlugin.getPlugin(Main.class);
    3. }
     
  14. Offline

    BlazingBroGamer

    Gater12
    Ahh I derped :3 I will use your method from now thanks
    EDIT: "this" cannot be used in static constructors I think
     
  15. Offline

    Gater12

  16. Offline

    BlazingBroGamer

    Gater12
    Errr this might be a bit off topic, but... the code you give gives an java.lang.IllegalStateException...
     
Thread Status:
Not open for further replies.

Share This Page