Solved NullPointerException when trying to get config file

Discussion in 'Spigot Plugin Development' started by garageagle, Oct 13, 2019.

Thread Status:
Not open for further replies.
  1. My plugin worked fine before I started using constructors.

    Code (Text):
    [17:32:53] [Server thread/ERROR]: Error occurred while enabling PTMCCore v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: null
            at me.garageagle.ptmccore.commands.GodCommand.<init>(GodCommand.java:17) ~[?:?]
            at me.garageagle.ptmccore.PTMCCore.onEnable(PTMCCore.java:26) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugin(CraftServer.java:461) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugins(CraftServer.java:375) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:449) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.DedicatedServer.init(DedicatedServer.java:258) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:776) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]

    Code (Java):
    getCommand("god").setExecutor(new GodCommand(this));

    Code (Java):
        PTMCCore plugin;
        public GodCommand(PTMCCore plugin) {
            this.plugin = plugin;
        }
     
        private String rawPrefix = plugin.getConfig().getString("raw-prefix");

    These aren't the full file of course.

    My IDE says
    Code (Text):
    Method invocation 'getConfig' will produce 'NullPointerException'
     
  2. Try change GodCommand.java to:
    Code (Text):
        private PTMCCore plugin;
        private String rawPrefix = "";

        public GodCommand(PTMCCore plugin) {
            this.plugin = plugin;
            rawPrefix = this.plugin.getConfig().getString("raw-prefix");
        }
     
  3. That solved it, thanks! :giggle:
     
Thread Status:
Not open for further replies.