Config Problem - java.lang.NullPointerException

Discussion in 'Spigot Plugin Development' started by KodyXtremeFTW, Jun 14, 2018.

  1. I was just about to test out some SG Plugin I finished coding & this NullPointerException keeps annoying me!
    I know that it's coming from the Core (Line 79) & ConfigHandler class (Line 45), but I'm confused a little with the cause. Any help would be great. :)

    Log:
    Code (Text):
    [10:37:35 INFO]: [SurvivalGames] Enabling SurvivalGames v1.0
    [10:37:35 INFO]: [SurvivalGames] Game Enabled!
    [10:37:35 ERROR]: Error occurred while enabling SurvivalGames v1.0 (Is it up to
    date?)
    java.lang.NullPointerException
            at me.SavageUser.SavageSG.ConfigHandler.<init>(ConfigHandler.java:45) ~[
    ?:?]
            at me.SavageUser.SavageSG.Core.onEnable(Core.java:79) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[S
    pigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:335) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.jav
    a:356) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.
    java:316) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:74
    6) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.Bukkit.reload(Bukkit.java:534) [Spigot.jar:git-Spigot-c3c7
    67f-33d5de3]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    25) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:646) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchServerCommand(Craf
    tServer.java:632) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.aM(DedicatedServer.java:
    353) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    17) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    34) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :537) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    ConfigHandler:

    Code (Text):
    package me.SavageUser.SavageSG;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;



    public class ConfigHandler {

        private Core plugin;
        public ConfigHandler(Core plugin) {
            this.plugin = plugin;
        }

        File file;
        FileConfiguration cfg;

        boolean chestRegen;
        int chestRegenTime;

        int timeBeforeShutdown;

        String staffMSG;

        List<Material> breakables;

        int minimumPlayers;



        public ConfigHandler(File f) {
            this.file = f;
            this.cfg = YamlConfiguration.loadConfiguration(f);

            chestRegen = cfg.getBoolean("chest-regen");
            timeBeforeShutdown = cfg.getInt("shutdown-time");
            staffMSG = cfg.getString("staff-join-message").replace("&", "§");
            minimumPlayers = cfg.getInt("minimum-players");
            breakables = new ArrayList<Material>();
            for (int id : cfg.getIntegerList("breakable-blocks")) {
                breakables.add(Material.getMaterial(id));
                System.out.println("Added: " + Material.getMaterial(id) + " to the breakable blocks list!");
            }

            World world = Bukkit.getServer().getWorld(cfg.getString("lobby.world"));
            double x = cfg.getDouble("lobby.x");
            double y = cfg.getDouble("lobby.y");
            double z = cfg.getDouble("lobby.z");
            float pitch = Float.parseFloat(cfg.getString("lobby.pitch"));
            float yaw = Float.parseFloat(cfg.getString("lobby.yaw"));
            Location loc = new Location(world, x, y, z);
            loc.setPitch(pitch);
            loc.setYaw(yaw);
            plugin.setLobby(loc);

            plugin.voteTime = cfg.getInt("vote-time");
        }



        public boolean hasChestRegen() {
            return chestRegen;
        }



        public int getChestRegenTime() {
            return chestRegenTime;
        }



        public int getTimeBeforeShutdown() {
            return timeBeforeShutdown;
        }



        public String getStaffMessage() {
            return staffMSG;
        }



        public List<Material> getBreakables() {
            return breakables;
        }



        public int getMinPlayers() {
            return minimumPlayers;
        }
    }
     
    Core (First 100 Lines):
    Code (Text):

    package me.SavageUser.SavageSG;

    import me.SavageUser.SavageSG.Commands.*;
    import me.SavageUser.SavageSG.Listeners.*;
    import me.SavageUser.SavageSG.Objects.*;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.WorldCreator;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    import java.io.*;
    import java.util.*;

    public class Core extends JavaPlugin {

        public static String TAG = "§6[SurvivalGames] §a";


        //Files & Configuration Files


        public static ConfigHandler cfgHandler;
        Game game;
        File rootDir;
        File playersDir;
        File mapDir;
        File config;
        File chestFile;

        Map<String, Tribute> players;
        boolean firstTime;
        List<String> voters;
        Map<String, GameMap> maps;
        Map<String, TempMap> tempMaps;
        Location lobby;

        Scoreboard sb;
        Objective votesObj;
        Score map1;
        Score map2;
        Score map3;
        Score map4;
        public int voteTime;

        List<String> worldNames;
        Set<String> frozen;
        List<Location> chests;
        List<ItemStack> tier1;
        List<ItemStack> tier2;

        //---------------------------

        @Override
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(TAG + "Game Enabled!");

            tier1 = new ArrayList<ItemStack>();
            tier2 = new ArrayList<ItemStack>();
            voters = new ArrayList<String>();
            maps = new HashMap<String, GameMap>();
            tempMaps = new HashMap<String, TempMap>();
            players = new HashMap<String, Tribute>();
            game = new Game();
            game.setGameTime(GameTime.Lobby);
            frozen = new HashSet<String>();
            chests = new ArrayList<Location>();

            init_files();
            cfgHandler = new ConfigHandler(config.getParentFile());
            worldNames = getConfig().getStringList("worlds");
            init_data();
            init_events();
            init_commands();
            init_chests();
            init_maps();

            File[] worlds = getServer().getWorldContainer().listFiles();
            for (File f : worlds)
                for (String w : worldNames){

                if (f.getName().equals(w)) {
                    if (Bukkit.getWorld(w) == null) {
                        try {
                            Bukkit.createWorld(new WorldCreator(w));
                            Bukkit.getServer().getConsoleSender().sendMessage(TAG + "Creating world " + w + " as it's null.");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    if (GameMap.getMap(w) == null) {
                        GameMap gm = new GameMap();
                        gm.setName(w);
                        gm.setLocation(Bukkit.getWorld(w).getSpawnLocation());
                        gm.save();
                        maps.put(w, gm);
                        Bukkit.broadcastMessage(TAG + "Map: " + w + " created. Setup is needed!");
                    }
                }

            }

            init_vote();
        }
     
     
  2. What's line 45 of your ConfigHandler class?
     
  3. staffMSG = cfg.getString("staff-join-message").replace("&", "§");
     
  4. You have to set config values before you get them
    right now, getString("staff-join-message") probably returns null,
    so doing replace() on it causes a NullPointerException
     
    • Agree Agree x 1
  5. Use Chatcolor.translatealternatecolorcodes if you want to replace color codes to colors.
     
    • Agree Agree x 2
  6. Strahan

    Benefactor

    Yup. You should always treat data from a config file as suspect. Assuming some moron admin broke your config and code the plugin so it handles it gracefully.

    Also be aware you can pass a default to the get methods for config so you could do like getString("staff-join-message", "") and it would not have shit itself then. Of course, that depends on whether or not the usage can be satisfied with a default value. If not, evaluate and act accordingly.
     
    • Agree Agree x 1
  7. I think he's calling a method to handle his chat color.
     
    • Agree Agree x 1
  8. Strahan

    Benefactor

    In his example he's doing a search and replace, which will work but is not recommended.
     
  9. Messed around with some of those ideas & fixed the staff-chat crap. Now I get this error:

    Code (Text):

    [14:10:10 INFO]: [SurvivalGames] Game Enabled!
    [14:10:10 INFO]: [SurvivalGames] Added: SAPLING to the breakable blocks list!
    [14:10:10 INFO]: [SurvivalGames] Added: LEAVES to the breakable blocks list!
    [14:10:10 INFO]: [SurvivalGames] Added: LONG_GRASS to the breakable blocks list!
    [14:10:10 INFO]: [SurvivalGames] Added: YELLOW_FLOWER to the breakable blocks li
    st!
    [14:10:10 INFO]: [SurvivalGames] Added: RED_ROSE to the breakable blocks list!
    [14:10:10 INFO]: [SurvivalGames] Added: BROWN_MUSHROOM to the breakable blocks l
    ist!
    [14:10:10 INFO]: [SurvivalGames] Added: RED_MUSHROOM to the breakable blocks lis
    t!
    [14:10:10 INFO]: [SurvivalGames] Added: VINE to the breakable blocks list!
    [14:10:10 ERROR]: Error occurred while enabling SurvivalGames v1.0 (Is it up to
    date?)
    java.lang.NullPointerException
            at me.SavageUser.SavageSG.ConfigHandler.<init>(ConfigHandler.java:77) ~[
    ?:?]
            at me.SavageUser.SavageSG.Core.onEnable(Core.java:79) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[S
    pigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:335) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.jav
    a:356) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.
    java:316) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:74
    6) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.Bukkit.reload(Bukkit.java:534) [Spigot.jar:git-Spigot-c3c7
    67f-33d5de3]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    25) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:646) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchServerCommand(Craf
    tServer.java:632) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.aM(DedicatedServer.java:
    353) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    17) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    34) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :537) [Spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
     
    ConfigHandler:
    Code (Text):
    package me.SavageUser.SavageSG;

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

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import static me.SavageUser.SavageSG.Core.TAG;


    public class ConfigHandler {

        private Core plugin;

        File file;
        FileConfiguration cfg;

        boolean chestRegen;
        int chestRegenTime;

        int timeBeforeShutdown;
        String staffMSG;

        List<Material> breakables;

        int minimumPlayers;



        public ConfigHandler(File f) {
            this.file = f;
            this.cfg = YamlConfiguration.loadConfiguration(f);

            this.plugin = plugin;

            chestRegen = cfg.getBoolean("chest-regen");
            timeBeforeShutdown = cfg.getInt("shutdown-time");
            if (cfg.getString("staff-join-message") == null) {
                cfg.set("staff-join-message", "");
            }
            staffMSG = cfg.getString("staff-join-message").replace("&", "§");
            minimumPlayers = cfg.getInt("minimum-players");
            breakables = new ArrayList<Material>();
            for (int id : cfg.getIntegerList("breakable-blocks")) {
                breakables.add(Material.getMaterial(id));
                Bukkit.getConsoleSender().sendMessage(TAG + "Added: " + Material.getMaterial(id) + " to the breakable blocks list!");
            }

            World world = Bukkit.getServer().getWorld(cfg.getString("lobby.world"));
            double x = cfg.getDouble("lobby.x");
            double y = cfg.getDouble("lobby.y");
            double z = cfg.getDouble("lobby.z");
            float pitch = Float.parseFloat(cfg.getString("lobby.pitch"));
            float yaw = Float.parseFloat(cfg.getString("lobby.yaw"));

            if (cfg.getString("lobby") == null) {
                try {
                    cfg.set("lobby.world", Bukkit.getWorld("world").getName());
                    cfg.set("lobby.x", Bukkit.getWorld("world").getSpawnLocation().getX());
                    cfg.set("lobby.y", Bukkit.getWorld("world").getSpawnLocation().getY());
                    cfg.set("lobby.z", Bukkit.getWorld("world").getSpawnLocation().getZ());
                    cfg.set("lobby.pitch", Bukkit.getWorld("world").getSpawnLocation().getPitch());
                    cfg.set("lobby.yaw", Bukkit.getWorld("world").getSpawnLocation().getYaw());
                    cfg.save(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Location loc = new Location(world, x, y, z);
            loc.setPitch(pitch);
            loc.setYaw(yaw);
            plugin.setLobby(loc);

            plugin.voteTime = cfg.getInt("vote-time");
        }



        public boolean hasChestRegen() {
            return chestRegen;
        }



        public int getChestRegenTime() {
            return chestRegenTime;
        }



        public String getStaffMessage() {
            return staffMSG;
        }



        public int getTimeBeforeShutdown() {
            return timeBeforeShutdown;
        }



        public List<Material> getBreakables() {
            return breakables;
        }



        public int getMinPlayers() {
            return minimumPlayers;
        }
    }

     
    Core:
    Code (Text):
    package me.SavageUser.SavageSG;

    import me.SavageUser.SavageSG.Commands.*;
    import me.SavageUser.SavageSG.Listeners.*;
    import me.SavageUser.SavageSG.Objects.*;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.WorldCreator;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    import java.io.*;
    import java.util.*;

    public class Core extends JavaPlugin {

        public static String TAG = "§6[SurvivalGames] §a";


        //Files & Configuration Files


        public static ConfigHandler cfgHandler;
        Game game;
        File rootDir;
        File playersDir;
        File mapDir;
        File config;
        File chestFile;

        Map<String, Tribute> players;
        boolean firstTime;
        List<String> voters;
        Map<String, GameMap> maps;
        Map<String, TempMap> tempMaps;
        Location lobby;

        Scoreboard sb;
        Objective votesObj;
        Score map1;
        Score map2;
        Score map3;
        Score map4;
        public int voteTime;

        List<String> worldNames;
        Set<String> frozen;
        List<Location> chests;
        List<ItemStack> tier1;
        List<ItemStack> tier2;

        //---------------------------

        @Override
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(TAG + "Game Enabled!");

            tier1 = new ArrayList<ItemStack>();
            tier2 = new ArrayList<ItemStack>();
            voters = new ArrayList<String>();
            maps = new HashMap<String, GameMap>();
            tempMaps = new HashMap<String, TempMap>();
            players = new HashMap<String, Tribute>();
            game = new Game();
            game.setGameTime(GameTime.Lobby);
            frozen = new HashSet<String>();
            chests = new ArrayList<Location>();

            init_files();
            cfgHandler = new ConfigHandler(config);
            worldNames = getConfig().getStringList("worlds");
            init_data();
            init_events();
            init_commands();
            init_chests();
            init_maps();

            File[] worlds = getServer().getWorldContainer().listFiles();
            for (File f : worlds)
                for (String w : worldNames){

                if (f.getName().equals(w)) {
                    if (Bukkit.getWorld(w) == null) {
                        try {
                            Bukkit.createWorld(new WorldCreator(w));
                            Bukkit.getServer().getConsoleSender().sendMessage(TAG + "Creating world " + w + " as it's null.");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    if (GameMap.getMap(w) == null) {
                        GameMap gm = new GameMap();
                        gm.setName(w);
                        gm.setLocation(Bukkit.getWorld(w).getSpawnLocation());
                        gm.save();
                        maps.put(w, gm);
                        Bukkit.broadcastMessage(TAG + "Map: " + w + " created. Setup is needed!");
                    }
                }

            }

            init_vote();
        }
     
  10. Referencing this line:
    Code (Text):
            plugin.setLobby(loc);
     
     
  11. You are setting "plugin" to itself at this part, and since its null its still going to be null
    and you can't call setLobby() on "null"
     
    • Agree Agree x 1

Share This Page