Solved Getting NPE when using custom config file

Discussion in 'Spigot Plugin Development' started by RoxioCZ, Feb 17, 2020.

  1. Hi, I am getting NPE for some reason when retrieving string from a file and I can't figure out why.

    Main class:
    Code (Java):

    package me.petr.bingo;

    import org.bukkit.plugin.java.JavaPlugin;

    public final class Bingo extends JavaPlugin {
        private static Bingo instance;
        private ConfigurationManager configurationManager;

        @Override
        public void onEnable() {
            instance = this;
            configurationManager = ConfigurationManager.getInstance();

            configurationManager.setup();
            initialize();

            getLogger().info(configurationManager.getMessages().getString("console.test"));
        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }

        public static Bingo getInstance() {
            return instance;
        }

        private void initialize() {

        }
    }
     
    ConfigurationManger class:
    Code (Java):
    package me.petr.bingo;

    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.io.IOException;

    public class ConfigurationManager {
        private Bingo plugin;
        public static ConfigurationManager instance = new ConfigurationManager();

        private ConfigurationManager() {
        }

        private File itemsFile, messagesFile;
        private FileConfiguration itemsConfig, messagesConfig;

        public static ConfigurationManager getInstance() {
            return instance;
        }

        public void setup() {
            plugin = Bingo.getInstance();

            plugin.saveDefaultConfig();
            createFile(itemsConfig, itemsFile, "items.yml");
            createFile(messagesConfig, messagesFile, "messages.yml");
        }

        public void createFile(FileConfiguration fileConfiguration, File file, String fileName) {
            file = new File(plugin.getDataFolder(), fileName);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                plugin.saveResource(fileName, false);
                plugin.getLogger().info("File " + fileName + " created successfully.");
            }

            fileConfiguration = new YamlConfiguration();
            try {
                fileConfiguration.load(file);
            } catch (IOException | InvalidConfigurationException e) {
                plugin.getLogger().severe("Couldn't create file " + fileName);
            }
        }

        public FileConfiguration getConfig() {
            return plugin.getConfig();
        }

        public FileConfiguration getMessages() {
            return messagesConfig;
        }

        public FileConfiguration getItems() {
            return itemsConfig;
        }
    }
     
    Error:
    Code (Text):
     Error occurred while enabling Bingo v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: null
            at me.petr.bingo.Bingo.onEnable(Bingo.java:17) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugin(CraftServer.java:461) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugins(CraftServer.java:375) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.reload(CraftServer.java:810) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.Bukkit.reload(Bukkit.java:576) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) [server.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchServerCommand(CraftServer.java:695) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:433) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:397) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:971) [server.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:816) [server.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
     
  2. SteelPhoenix

    Moderator

    Your file and fileconfiguration fields are never assigned a value and are therefore null.
     
  3. In the createFile() method?
     
  4. SteelPhoenix

    Moderator

    The fields in the ConfigurationManager class do not get assigned a value (file and fileconfiguration fields).
     
    • Winner Winner x 1
  5. Thank you. That was a dumb mistake.