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



    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".
    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.)
            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.
    [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( ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand( ~[spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$ [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_151]
        at 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( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C( [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at [spigot.jar:git-Spigot-b6ecf3b-fb423b8]
        at Source) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
        at me.dessie.customenchants.inventoryListener.configHandler( ~[?:?]
        at me.dessie.customenchants.Commands.onCommand( ~[?:?]
        at org.bukkit.command.PluginCommand.execute( ~[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


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


    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:

    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


Thread Status:
Not open for further replies.

Share This Page