1.13.2 NullPointerException when trying to call getConfig()

Discussion in 'Spigot Plugin Development' started by Skptical, Apr 3, 2020.

  1. So basically when ever I try get a string from the config it always returns this error these are my main classes and the error:


    Error:

    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'hoh' in plugin HideOrHunt v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:48) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:704) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1621) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1461) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:9) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
            at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:896) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
            at me.skptical.hideorhunt.commands.HoH.onCommand(HoH.java:71) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            ... 15 more
    >deop skptical
    [20:16:20 INFO]: Made Skptical no longer a server operator
    [20:16:25 INFO]: SkpticalPvP issued server command: /hoh
    [20:16:28 INFO]: SkpticalPvP issued server command: /hoh SkpticalPvP
    [20:16:32 INFO]: SkpticalPvP issued server command: /hoh createteam
    [20:16:33 INFO]: SkpticalPvP issued server command: /hoh createteam
    [20:16:33 INFO]: SkpticalPvP issued server command: /hoh createteam
    [20:16:37 INFO]: SkpticalPvP issued server command: /lp
    [20:16:38 INFO]: SkpticalPvP issued server command: /pl
    [20:16:41 INFO]: SkpticalPvP issued server command: /deop skptical
    [20:16:44 INFO]: SkpticalPvP issued server command: /deop skpticalpvp
    [20:16:44 INFO]: [SkpticalPvP: Made SkpticalPvP no longer a server operator]
    [20:16:46 INFO]: SkpticalPvP issued server command: /hoh
    [20:16:46 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hoh' in plugin HideOrHunt v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:48) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:704) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1621) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1461) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:9) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
            at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:896) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot.jar:git-Spigot-1a3504a-84f3da3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
            at me.skptical.hideorhunt.commands.HoH.onCommand(HoH.java:44) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
            ... 15 more
    Main Class:

    Code (Java):
    package me.skptical.hideorhunt.main;

    import me.skptical.hideorhunt.commands.HoH;
    import me.skptical.hideorhunt.listeners.BlockBreakListener;
    import me.skptical.hideorhunt.listeners.BlockPlaceListener;
    import me.skptical.hideorhunt.listeners.PlayerChatListener;
    import me.skptical.hideorhunt.listeners.PlayerDeathListener;
    import org.bukkit.Bukkit;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class HideOrHunt extends JavaPlugin {

        private static HideOrHunt instance;
        private PluginManager pm;
        private ConsoleCommandSender console;
        private API api = new API();

        private String version = this.getDescription().getVersion();
        private FileManager fm;


        public static HideOrHunt getInstance() {
            return instance;
        }

        @Override
        public void onEnable(){
            instance = this;
            fm = new FileManager();
            console = Bukkit.getConsoleSender();
            console.sendMessage(api.getColored("&b&lHide&7&lOr&c&lHunt &aPlugin has been successfully enabled and loaded."));
            console.sendMessage(api.getColored("&b&lHide&7&lOr&c&lHunt &aIf you encounter any errors please contact Skptical on SpigotMC.org"));
            fm.saveFromJarConfig();
            fm.reloadConfig();
            fm.saveFromJarTeams();
            fm.reloadTeams();
            registerCommands();
            registerEvents();



        }


        @Override
        public void onDisable(){
            console = Bukkit.getConsoleSender();
            console.sendMessage(api.getColored("&b&lHide&7&lOr&c&lHunt &4Plugin has been successfully disabled and unloaded"));
            console.sendMessage(api.getColored("&b&lHide&7&lOr&c&lHunt &4If you encounter any errors please contact Skptical on SpigotMC.org"));



        }


        private void registerCommands(){
            this.getCommand("hoh").setExecutor(new HoH());

        }

        private void registerEvents(){
            pm = this.getServer().getPluginManager();
            pm.registerEvents(new BlockBreakListener(), this);
            pm.registerEvents(new BlockPlaceListener(), this);
            pm.registerEvents(new PlayerDeathListener(), this);
            pm.registerEvents(new PlayerChatListener(), this);


        }




    }
     

    Command class:

    Code (Java):
    package me.skptical.hideorhunt.commands;

    import me.skptical.hideorhunt.main.API;
    import me.skptical.hideorhunt.main.FileManager;
    import me.skptical.hideorhunt.main.HideOrHunt;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    public class HoH implements CommandExecutor{

        public HoH(){



        }

        private API api = new API();
        private FileManager fm = new FileManager();
        private HideOrHunt hoh = HideOrHunt.getInstance();



        public void sendHelpMessage(Player target, Integer page){
            target.sendMessage(api.getColored("&e&m----------  &b&lHide&7&lOr&c&lHunt  &e&m----------\n&d/hoh help &e- &eShows this page.\n&d/hoh createteam <name> &e- &eCreate a new team. "));
            return;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(label.equalsIgnoreCase("hoh") || label.equalsIgnoreCase("hideorhunt")){

                String invalidEntity = api.getColored("&cInvalid entity type only players can execute this command.");

                if(sender instanceof Player){
                    Player p = (Player) sender;
                    if(args.length == 0 || args[0].equalsIgnoreCase("help")){
                        if(p.hasPermission("hoh.help")){
                            sendHelpMessage(p, 1);
                        }else{
                            p.sendMessage(api.getColored(fm.getConfig().getString("permission-message", "&cSorry but do you not have the correct permission to perform this action!")));
                        }
                        return true;


                    }else if(args.length == 1){
                        if(args[0].equalsIgnoreCase("help")){
                            if(p.hasPermission("hoh.help")){
                                sendHelpMessage(p, 1);
                            }else{
                                p.sendMessage(api.getColored(fm.getConfig().getString("permission-message", "&cSorry but do you not have the correct permission to perform this action!")));
                            }
                            return true;

                        }else if(args[0].equalsIgnoreCase("createteam")){
                            if(p.hasPermission("hoh.teams.create")){
                                p.sendMessage(api.getColored("&cInvalid arguments! Usage: \n/hoh createteam <team name>"));
                            }else{
                                p.sendMessage(api.getColored(fm.getConfig().getString("permission-message", "&cSorry but do you not have the correct permission to perform this action!")));

                            }
                            return true;
                        }

                    }else if(args.length == 2){
                        if(args[0].equalsIgnoreCase("createteam")){
                            if(p.hasPermission("hoh.teams.create")){
                                if(fm.getTeams().contains(args[1])){
                                    p.sendMessage(api.getColored("&cThe team &e" + args[1] + "&c already exists."));
                                }else{
                                    fm.getTeams().createSection(args[1]);
                                    p.sendMessage(api.getColored("&aYou've successfully created the team " + args[1] + "."));
                                }
                                return true;
                            }else{
                                p.sendMessage(api.getColored(fm.getConfig().getString("permission-message", "&cSorry but do you not have the correct permission to perform this action!")));
                            }
                            return true;
                        }

                    }



                }else{
                    sender.sendMessage(invalidEntity);
                    return true;
                }



                return true;
            }
            return false;
        }
    }
     

    File Manager class:

    Code (Java):
    package me.skptical.hideorhunt.main;

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

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;

    public class FileManager {

        public FileManager fileManager;
        public File configFile;
        public FileConfiguration configConfiguration;
        public String configName = "config.yml";
        public File teamsFile;
        public FileConfiguration teamsConfig;
        public String teamsName = "teams.yml";
        public HideOrHunt plugin = HideOrHunt.getPlugin(HideOrHunt.class);

        public FileManager() {
        }

        public void createConfig() {
            if (!this.plugin.getDataFolder().exists()) {
                this.plugin.getDataFolder().mkdir();
            }

            this.configFile = new File(this.plugin.getDataFolder(), this.configName);
            if (!this.configFile.exists()) {
                try {
                    this.configFile.createNewFile();
                } catch (IOException var2) {
                    this.plugin.getLogger().severe("ERROR: THERE WAS AN ERROR CREATING THE " + this.configName + " FILE: ");
                    var2.printStackTrace();
                }
            }

            this.configConfiguration = YamlConfiguration.loadConfiguration(this.configFile);
        }

        public FileConfiguration getConfig() {
            return this.configConfiguration;
        }

        public void saveConfig() {
            try {
                this.configFile = new File(this.plugin.getDataFolder(), this.configName);
                this.configConfiguration.save(this.configFile);
            } catch (IOException var2) {
                this.plugin.getLogger().severe("ERROR: THERE WAS AN ERROR SAVING THE " + this.configName + " FILE: ");
                var2.printStackTrace();
            }

        }

        public void reloadConfig() {
            this.configConfiguration = YamlConfiguration.loadConfiguration(this.configFile);
        }

        public void saveFromJarConfig() {
            this.configFile = new File(this.plugin.getDataFolder(), this.configName);
            if (!this.configFile.exists()) {
                this.plugin.saveResource(this.configName, false);
            }

        }

        public void createTeams() {
            if (!this.plugin.getDataFolder().exists()) {
                this.plugin.getDataFolder().mkdir();
            }

            this.teamsFile = new File(this.plugin.getDataFolder(), this.teamsName);
            if (!this.teamsFile.exists()) {
                try {
                    this.teamsFile.createNewFile();
                } catch (IOException var2) {
                    this.plugin.getLogger().severe("ERROR: THERE WAS AN ERROR CREATING THE " + this.teamsName + " FILE: ");
                    var2.printStackTrace();
                }
            }

            this.teamsConfig = YamlConfiguration.loadConfiguration(this.teamsFile);
        }

        public FileConfiguration getTeams() {
            return this.teamsConfig;
        }

        public void saveTeams() {
            try {
                this.teamsFile = new File(this.plugin.getDataFolder(), this.teamsName);
                this.teamsConfig.save(this.teamsFile);
            } catch (IOException var2) {
                this.plugin.getLogger().severe("ERROR: THERE WAS AN ERROR SAVING THE " + this.teamsName + " FILE: ");
                var2.printStackTrace();
            }

        }

        public void reloadTeams() {
            this.teamsConfig = YamlConfiguration.loadConfiguration(this.teamsFile);
        }

        public void saveFromJarTeams() {
            this.teamsFile = new File(this.plugin.getDataFolder(), this.teamsName);
            if (!this.teamsFile.exists()) {
                this.plugin.saveResource(this.teamsName, false);
            }

        }


    }
     
     
  2. drives_a_ford

    Moderator

    The two FileManager instances (one in HideOrHunt and the other in HoH) are just that - two separate instances.
    You probably want to share them. Use dependency injection to pass the instance from HideOrHunt to HoH.

    But in all honesty, the FileManager class should be redesigned. If you're going to call a bunch of methods after creating the instance, might as well take care of these things in its constructor.
     
  3. How would do that? Sorry but I'm kinda new to constructors not gunna lie.
     
  4. I would try using getConfig(); in the main class and passing it in as a parameter in the constructor for FileManager instead. At least for the main config. You can also use
    Code (Text):
    FileConfiguration teams = (FileConfiguration) YamlConfiguration.loadConfiguration(teamsFile));
    to load it.