Problem while using a Utility class.

Discussion in 'Plugin Development' started by Banjer_HD, Nov 4, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Banjer_HD

    Problem: I got an error when I start the server, It is caused on this line:
    Code:
    getCommand("gamemode").setExecutor(new gamemode(new Utils(this)));

    Code:
    Main:

    Code:
    public class Main extends JavaPlugin {
      
        File f = new File(getDataFolder() + "/language.yml");
        YamlConfiguration configUtils = YamlConfiguration.loadConfiguration(f);
      
        public void onEnable() {
            loadConfig();
          
            getCommand("gamemode").setExecutor(new gamemode(new Utils(this)));
        }
      
        public void onDisable() {
          
        }
      
        public void loadConfig() {
          
            configUtils.addDefault("prefix", "&e[&8Server Name&e]&r");
            configUtils.addDefault("onlyPlayersCanDoThis", "&4Only players can do this!");
            configUtils.addDefault("noPermission", "&4You don't have the right permissions to do this.");
          
            configUtils.options().copyDefaults(true);
            saveLanguageFile();
        }
    
    gamemode:
    Code:
    public class gamemode implements CommandExecutor {
      
        private Utils utils;
        public gamemode(Utils ut) {
            this.utils = ut;
        }
      
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("gamemode")) {
                if(sender.hasPermission("utilities.gamemode")) {
                    if(args.length == 1) {
                        if(sender instanceof Player) {
                          
                            Player p = (Player) sender;
                          
                            if(args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("1")) {
                                p.setGameMode(GameMode.CREATIVE);
                            }
                          
                            if(args[0].equalsIgnoreCase("survival") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("0")) {
                                p.setGameMode(GameMode.SURVIVAL);
                            }
                          
                          
                        }else {
                            sender.sendMessage(utils.onlyPlayersCanDoThis);
                        }
                    }
                }else {
                    sender.sendMessage(utils.noPermission);
                }
            }
            }
          
            return false;
        }
    }
    
    Utils:
    Code:
    public final class Utils {
      
        private Main plugin;
        public Utils(Main plugin) {
            this.plugin = plugin;
        }
      
        private Utils(){}
      
        public final String prefix = plugin.configUtils.getString("prefix") + " ";
      
        public final String onlyPlayersCanDoThis = prefix + plugin.configUtils.getString("onlyPlayersCanDoThis");
        public final String noPermission = prefix + plugin.configUtils.getString("noPermission");
    }
    
    Possible problematic line(s):
    Code:
    getCommand("gamemode").setExecutor(new gamemode(new Utils(this)));

    The FULL Error log:
    Code:
    [17:12:06 ERROR]: Error occurred while enabling Utilities v0.1 (Is it up to date?)
    java.lang.NullPointerException
            at net.Utilities.Utils.<init>(Utils.java:12) ~[?:?]
            at net.Utilities.Main.onEnable(Main.java:22) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:362) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:322) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(CraftServer.java:746) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.Bukkit.reload(Bukkit.java:539) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at net.minecraft.server.v1_10_R1.DedicatedServer.aL(DedicatedServer.java:437) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:673) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:572) [spigot-1.10.2.jar:git-Spigot-f09019b-a625e45]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    
     
  2. Offline

    timtower Moderator Moderator

    @Banjer_HD Bad utils class.
    It is making calls when the constructor isn't called yet.
     
  3. Offline

    Banjer_HD

    Thanks for your reply!
    Do you know how to fix that?
     
  4. Offline

    ipodtouch0218

    Code:
        public final String prefix = plugin.configUtils.getString("prefix") + " ";
    
        public final String onlyPlayersCanDoThis = prefix + plugin.configUtils.getString("onlyPlayersCanDoThis");
        public final String noPermission = prefix + plugin.configUtils.getString("noPermission");
    All these are using the "plugin" var before the constructor gets to it. Move it into the Constructor.

    Initialize them outside the constructor, then set the values inside, like you did for "plugins"
     
    Last edited: Nov 4, 2016
  5. Offline

    Banjer_HD

    Thanks for your reply!!
    You mean like this?
    Code:
    public final class Utils {
       
        private Main plugin;
        public Utils(Main plugin) {
            this.plugin = plugin;
        }
       
        private Utils(){}
       
        public static String prefix;
       
        public String onlyPlayersCanDoThis;
        public String noPermission;
        public String useSomethingAfterGamemode;
       
       
        public void loadUtils() {
           
            prefix = plugin.configUtils.getString("prefix") + " ";
           
            onlyPlayersCanDoThis = prefix + plugin.configUtils.getString("onlyPlayersCanDoThis");
            noPermission = prefix + plugin.configUtils.getString("noPermission");
            useSomethingAfterGamemode = prefix + plugin.configUtils.getString("useSomethingAfterGamemode");
           
        }
    }
    
    And where to run loadUtils()?
    Thank you in andvance!!
     
  6. Offline

    timtower Moderator Moderator

  7. Offline

    Banjer_HD

    Thanks for your reply!
    If I use "player.sendMessage(Utils.noPermission);" It returns null... (I am seeing a blank chat line)

    My code:
    Code:
    private Utils(){ loadUtils(); }
       
        public static String prefix;
       
        public String onlyPlayersCanDoThis;
        public String noPermission;
        public String useSomethingAfterGamemode;
       
       
        public void loadUtils() {
           
            prefix = plugin.configUtils.getString("prefix") + " ";
           
            onlyPlayersCanDoThis = prefix + plugin.configUtils.getString("onlyPlayersCanDoThis");
            noPermission = prefix + plugin.configUtils.getString("noPermission");
            useSomethingAfterGamemode = prefix + plugin.configUtils.getString("useSomethingAfterGamemode");
           
        }
    
    Thank you for reading!
     
  8. Offline

    Zombie_Striker

    @Banjer_HD
    The code from your last post shows you have two constructors. Do you call loadUtils for both constructors? Also, are you sure you create the utils class before you send the message?
     
  9. Offline

    Banjer_HD

    Thanks for your reply @Zombie_Striker ! I dont think I created the Utils class before sending the message.... Do you know where to do that?

    Thanks! :)
     
  10. Offline

    Zombie_Striker

    @Banjer_HD
    You should be creating the util object in the onEnable.

    [Edit] Just re-read the util. The problem is that it is still treated as a regular object instead of a singleton class. All of the field and that one method should have been static. Add the static modifier to all the fields and methods to fix the problem.
     
Thread Status:
Not open for further replies.

Share This Page