Solved Null pointer exception, except should work fine...

Discussion in 'Spigot Plugin Development' started by Vhbob, Jun 30, 2018.

  1. Hello, how would I go about saving a custom config file? I have created it in my main class:
    Code (Text):
    private File pluginData;
        private FileConfiguration dataConfig;

    public FileConfiguration getCustomConfig() {
            return this.dataConfig;
        }

        private void createCustomConfig() {
            pluginData = new File(getDataFolder(), "pluginData.yml");
            if (!pluginData.exists()) {
                pluginData.getParentFile().mkdirs();
                saveResource("pluginData.yml", false);
            }

            dataConfig = new YamlConfiguration();
            try {
                dataConfig.load(pluginData);
            } catch (IOException | InvalidConfigurationException event) {
                event.printStackTrace();
            }
        }
    and made created an instance of it in my other class:
    Code (Text):
    FileConfiguration configData = Main.getPlugin(Main.class).getCustomConfig();
    But I can't seem to find a way to save the config.

    See my latest post for update on the issue
     
    #1 Vhbob, Jun 30, 2018
    Last edited: Jul 1, 2018
  2. I have tried the following:
    Code (Text):
    File pluginData = new File(Main.getPlugin(Main.class).getDataFolder(), "pluginData.yml");

    try {
                            configData.save(pluginData);
                        } catch (IOException ex) {
                            // TODO Auto-generated catch block
                            ex.printStackTrace();
                        }
     
  3. Use the “File” object built into Java.
     
  4. Here are the errors im getting:
    Code (Text):
    [18:11:06 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'forge' in plugin TheForge v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:649) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:324) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at com.vhbob.tf.commands.CreateForge.onCommand(CreateForge.java:34) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            ... 15 more
    Heres the line of code:
    Code (Text):
    String prefix = ChatColor.translateAlternateColorCodes('&', pl.getConfig().getString("prefix"))
                    + ChatColor.RESET;
    and the config:
    Code (Text):
    prefix: "&7[&EThe Forge&7]: "
     
  5. Updated title
     
  6. Code (Text):
    pl.getConfig().getString("prefix")
    This is the piece of code returning null. This means it's not finding prefix in the yaml, so its not loaded correctly into memory from the config. You'll have to find out why, or show us more code so we can find out why. Its really hard to find a bug in something from 1 line of code.

    Also, for future reference you can use getString("prefix", "") to remove the NPE. The 2nd String param is a "default" in case the key isn't found in the config. You can set it to something more meaningful like getString("prefix", "NOT_FOUND") if you want, or leave it as empty String.

    But the root cause is prefix isnt found.
     
  7. I am using a custom config. However the one im needing to reference is just the default config so getconfig should suffice no?
     
  8. No idea why but I deleted everything in my plugins folder, re-exported it and now it works like a charm.