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()) {
                saveResource("pluginData.yml", false);

            dataConfig = new YamlConfiguration();
            try {
            } catch (IOException | InvalidConfigurationException event) {
    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 {
                        } catch (IOException ex) {
                            // TODO Auto-generated catch block
  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( ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand( ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$ [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_161]
            at 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( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C( [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at org.bukkit.ChatColor.translateAlternateColorCodes( ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at ~[?:?]
            at org.bukkit.command.PluginCommand.execute( ~[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):
    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.