Solved NPE when calling getConfig

Discussion in 'Spigot Plugin Development' started by TheOpGaming, Nov 10, 2018.

  1. Dont know whats happening here
    Code (Text):
    [20:13:15 ERROR]: Error occurred while enabling SMPUtilities v0.1A (Is it up to date?)
    java.lang.NullPointerException
            at java.lang.String.<init>(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:162) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:124) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at me.jonesy.smp.SMPUtilities.onEnable(SMPUtilities.java:19) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:273) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload(Bukkit.java:238) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    onEnable:

    Code (Text):
    public void onEnable() {
            saveDefaultConfig();
            inst = this;
            cfg = getConfig();
            csmp = new CommandSMP();
            getCommand("smp").setExecutor(csmp);
        }
     
  2. Can you tell us what you have on line 19 of your code? That's where the error is located.
     
  3. This is line 19
    Code (Text):
    cfg = getConfig();
    When i use getConfig at all, it throws an NPE
     
  4. You need to set the variable to something. Forgot what it was, but I think it was something like fileConfiguration.
    So you would have
    FileConfiguration cfg = getConfig ();
     
  5. I have already created cfg outside of onEnable and it is already FileConfiguration, so im confused
     
  6. Provide us with full code.
     
    • Agree Agree x 1
  7. Where are you defining what cfg is? (Also, no reason to assign it instead use a config section if you need quicker access)
     
  8. Changed my code, removed getConfig() from onEnable(),

    it is now in my command class

    code:
    Code (Text):
    public class CommandSMP implements CommandExecutor {
        private SMPUtilities util;
        public CommandSMP(SMPUtilities inst) {
            util=inst;
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!cmd.getName().equalsIgnoreCase("smp")) {
                return false;
            }

            //SMPUtilities util = SMPUtilities.inst;
            if (!sender.hasPermission(util.getConfig().getString("commands.smp.permission"))) {
                sender.sendMessage(util.PREFIX + util.getConfig().getString("messages.no-permission"));
                util = null;
                return true;
            }
        return false;
        }
    }
    config.yml:
    Code (Text):
    messages:
        prefix: "&bSMP &f> &b"
        no-permission: "&cSorry, you dont have permission to use that command."
        config-reloaded: "&aThe configuration file has been reloaded."

    commands:
        smp:
            permission: smputilities.smp
    new error
    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'smp' in plugin SMPUtilities v0.1A
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at java.lang.String.<init>(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:162) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:124) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at me.jonesy.smp.commands.CommandSMP.onCommand(CommandSMP.java:27) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 8 more
    Line 27:
    Code (Java):
    if (!sender.hasPermission(util.getConfig().getString("commands.smp.permission"))) {
     
  9. 1)
    Code (Java):
    util = null;
    Why?

    2) full code means code from your javaplugin class also.
     
  10. removed util=null and heres main class
    Code (Java):

    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.jonesy.smp.commands.CommandSMP;

    public class SMPUtilities extends JavaPlugin implements Listener {
        public static SMPUtilities inst;
        public String PREFIX = "&bSMP &f> &b";
        private CommandSMP csmp;

        public void onEnable() {
            saveDefaultConfig();
            inst = this;
            csmp = new CommandSMP(this);
            getCommand("smp").setExecutor(csmp);
        }

    }
     
     
  11. Did you register the command in the plugin yml?
     
  12. yes i did
    Code (Text):
    commands:
        smp:
            description: Base plugin command for SMPUtilities.
            aliases: [smputilities]
            usage: /<command> [args]
     
  13. You never initiate your SMPUtilities constructor.

    Code (Java):
    public static SMPUtilities inst;
    Replace this with:
    Code (Java):
    private final SMPUtilities inst = new SMPUtilities(this);
    Or just
    Code (Java):
    new SMPUtilities(this);
    on your onEnable method.
     
  14. why are you implementing a Listener in that class and not registering events? (I know this is outside the context but helps)
    Why do you not have a class dedicated to Listening events..
     
  15. i always implement listener for some reason, idk why i do it, just habit i guess
     
  16. Code (Text):
    [20:40:11 WARN]: Unexpected exception while parsing console command "smp"
    dunno what that means
     
  17. i dont have a constructor for SMPUtilities, that takes SMPUtilities... Kind of stupid imo, besides, i set inst in onenable
     
  18. Okx

    Okx

    No, that's just going to throw an error itself. You can't instantiate a JavaPlugin.
     
  19. [Solved]
    spigot jar file mustve been corrupted or something, replaced it with a fresh one and it works
     
  20. Ah true, I got confused by the class's name :unsure:
     

Share This Page