Can't get Config from different class.

Discussion in 'Plugin Development' started by Dessie_YT, Aug 16, 2018.

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

    Dessie_YT

    Hello.

    I'm trying to access my config from a class other than my Main(which extends JavaPlugin).

    The configuration loads successfully from the config.yml I wrote, and IntelliJ shows no errors when building.
    However, when I try to access the command I'm getting a NPE and I don't know why.

    Listener (open)

    Yes, my main class is called "Main" and my Listener is called "inventoryListener".
    Code:
       
    private Main plugin;
        public inventoryListener(Main plugin) {
            this.plugin = plugin;
        }

    This is the line the StackTrace says is throwing the NPE. (I narrowed it down to this atleast.)
    Code:
            FileConfiguration config = plugin.getConfig();


    StackTrace (open)


    Also I know that it says Line 20 in Commands. That's just where it's calling the method from inventoryListener.
    Code:
    [15:04:10] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'enchants' in plugin CustomEnchants v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
        at me.dessie.customenchants.inventoryListener.configHandler(inventoryListener.java:27) ~[?:?]
        at me.dessie.customenchants.Commands.onCommand(Commands.java:20) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        ... 15 more
    


    I've looked at all the threads I could find on calling the config from another class. However I can't seem to get it to call properly.
     
  2. Offline

    KarimAKL

    @Dessie_YT This works for me, maybe it would help if you showed the whole class?
     
  3. Offline

    MrGeneralQ


    Did you send an instance of the Main class in the parameters?
    Is the code in your onEnable() on your main class?

    It should look something like this:


    Code:
    @Override
    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvent(new inventoryListener(this), this);
    }
    The NPE can only be thrown in your case if the data is not sent, so either not registered in your onEnable() or you did not send the instance of your main class trough the constructor. (that would however already give you a build error)
     
  4. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page