Lang file .toString() NullPointer

Discussion in 'Plugin Development' started by Kevinzuman22, Jun 18, 2017.

Thread Status:
Not open for further replies.
  1. I have implemented language files into my plugin, and I've added all the messages to my commands. However, when defining the variable 'prefix' in my main command handler, it gived a NullPointerException in my Lang class on line 97, which is the toString() function.

    Lang file:
    Lang class (open)

    Code:
    package me.greenadine.worldspawns;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public enum Lang {
        PREFIX("prefix", "&9[WorldSpawns]"),
        COMMAND_NOARGS("command-no-args", "&fWorldSpawns plugin version %versionid% made by Kevinzuman22. Type &7/%label% help or &7/%label& ? &ffor a list of commands."),
        COMMAND_RELOAD_FAIL("command-reload-fail", "&fFailed to reload plugin! Report this problem to plugin developer and/or the server administrator(s)."),
        COMMAND_RELOAD_SUCCESS("command-reload-success", "&fSuccessfuly reloaded plugin!"),
        COMMAND_UPDATE_SUCCESS("command-update-success", "&fAn update has been found. Restart or reload the server to update the plugin."),
        COMMAND_UPDATE_NO_UPDATE("command-update-no-update", "&fNo update was found."),
        COMMAND_UPDATE_FAIL_DOWNLOAD("command-update-fail-download", "&cCould not download new update! Check for internet connection."),
        COMMAND_UPDATE_FAIL_NOVERSION("command-update-fail-noversion", "&cUnable to detect newest version on BukkitDev. Report this to the plugin developer."),
        COMMAND_UPDATE_FAIL_DBO("command-update-fail-dbo", "&cUnable to reach BukkitDev. No internet connection/DevBukkit website is offline."),
        COMMAND_UPDATE_FAIL_APIKEY("command-update-fail-apikey", "&cInvalid apikey defined in config."),
        COMMAND_UPDATE_FAIL_BADID("command-update-fail-badid", "&cInvalid resource ID. Report this to the plugin developer."),
        COMMAND_UPDATE_FAIL_UNKNOWN("command-update-fail-unknown", "&cUnknown error. Report this to the plugin developer."),
        COMMAND_UPDATE_DISABLED("command-update-disabled", "&eUpdater has been disabled. Enable it in the plugin's config file."),
        COMMAND_HELP_HEADER("command-help-header", "&9&m-->&r &fWorldSpawns Help &9&m<--"),
        COMMAND_HELP_NOTE("command-help-note", "&7NOTE: [] = required, <> = optional."),
        COMMAND_HELP_SPAWNS_DESC("command-help-spawns-desc", "&7/%label% spawns - &fShows a list of spawns-related commands."),
        COMMAND_HELP_HUB_DESC("command-help-hub-desc", "&7/%label% hub - &fShows a list of hub-related commands."),
        COMMAND_HELP_RELOAD_DESC("command-help-reload-desc", "&7/%label% reload - &fReload the plugin and it's config."),
        COMMAND_HELP_CHECK_DESC("command-help-check-desc", "&7/%label% check [worldname]|shub- &fCheck where the spawn is located of a world."),
        COMMAND_HELP_RESET_DESC("command-help-reset-desc", "&7/%label% reset - &fReset the spawn of all worlds."),
        COMMAND_HELP_SPAWNS_SPAWN_DESC("command-help-spawn-spawn-desc", "&7/spawn - &fTeleport to your current world's spawn, or another world's spawn"),
        COMMAND_HELP_SPAWNS_SETSPAWN_DESC("command-help-spawn-setspawn-desc", "&7/setspawn - &fSet the spawn of your current world."),
        COMMAND_HELP_SPAWNS_DELSPAWN_DESC("command-help-spawn-delspawn-desc", "&7/delspawn - &fDelete the spawn of your current world, or another world's spawn."),
        COMMAND_HELP_HUB_ENABLEHUB_DESC("command-help-hub-enablehub-desc", "&7/%label% enablehub [true|false] - Enable or disable /hub, (dis)allowing players to teleport to the hub."),
        COMMAND_HELP_HUB_HUB_DESC("command-help-hub-hub-desc", "&7/hub <player> - &fTeleport yourself or another player to the server's hub."),
        COMMAND_HELP_HUB_SETHUB_DESC("command-help-hub-sethub-desc", "&7/sethub - &fSet the server's hub to your current location."),
        COMMAND_HELP_HUB_RESETHUB_DESC("command-help-hub-resethub-desc", "&7/resethub - &fReset the server's hub."),
        COMMAND_TOO_MANY_ARGUMENTS("command-too-many-arguments", "&cToo many arguments. Usage: &6/%label% [subcommand]&c."),
        COMMAND_NO_PERMISSION("command-no-permission", "&cYou have no permission to preform this action."),
        CHECK_HEADER_HUB("check-header-hub", "&9--> &fWorldSpawns Check - Hub &9<--"),
        CHECK_HEADER_WORLD("check-header-world", "&9--> &fWorldSpawns Check - Spawn of &6%worldname% &9<--"),
        PLAYER_JOIN_HEADER("player-join-header","&9&m----------->&r &fClockSign &9&m<-----------"),
        PLAYER_JOIN_VERSION("player-joinnotification-version", "&fVersion: &6%versionid%&f."),
        PLAYER_JOIN_MCVERSION("player-joinnotification-mcversion", "&fFor Minecraft: &6%mcversionid%&f."),
        PLAYER_JOIN_CBVERSION("player-joinnotification-cbversion", "&fCraftbukkit: &6%cbversionid%&f."),
    
        COMMAND_CHECK_DEFINE("command-check-define", "&cPlease define what to check the location from. Usage: &6/%label% check [worldname]|hub&c."),
        COMMAND_RESET_SUCCESS("command-reset-success", "&fAll spawns have been reset."),
        COMMAND_RESET_FAIL("command-reset-fail", "&cFailed to reset spawns. See console for information."),
        COMMAND_ENABLEHUB_INCORRECT("command-enablehub-incorrect", "&cIncorrect command usage. Usage: &6/%label% enablehub [true|false]&c."),
        COMMAND_UNKNOWN_SUBCOMMAND("command-unknown-subcommand", "&cUnknown subcommand '%args%'. Use &6/%label% help &cor &6/%label ? &cfor a list of commands."),
        COMMAND_HELP_TOOMANYARGS("command-help-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%/label% help&c."),
        COMMAND_RELOAD_TOOMANYARGS("command-reload-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%/label% reload&c."),
        COMMAND_RESET_TOOMANYARGS("command-reset-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%/label% reset&c."),
        COMMAND_SPAWNS_TOOMANYARGS("command-spawns-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%/label% spawns&c."),
        COMMAND_CHECK_TOOMANYARGS("command-reset-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%/label% reset&c."),
        COMMAND_ENABLEHUB_TOOMANYARGS("command-enablehub-toomanyargs", "&cToo many arguments (%lenght%). Usage: &6%label% enablehub [true|false]&c."),
        COMMAND_CHECK_HUB_NOTSET("command-check-hub-notset", "&fThe hub hasn't been set yet."),
        COMMAND_CHECK_INAIR_TRUE("command-check-inair-true", "&7In air: &ftrue&7."),
        COMMAND_CHECK_INAIR_FALSE("command-check-inair-false", "&7In air: &ffalse&7."),
        COMMAND_CHECK_WORLD_NULL("command-check-world-null", "&cWorld &6'%worldname%' &cdoesn't exist!"),
        COMMAND_CHECK_WORLD_SPAWN_NULL("command-check-world-spawn-null", "&fThe spawn of this world has not been set yet."),
        COMMAND_ENABLEHUB_SET("command-enablehub-set", "&fHub %boolean%&f."),
        COMMAND_ENABLEHUB_WRONGBOOL("command-enablehub-wrongbool", "&cThis can only be set to either 'true' or 'false'.");
       
       
       
       
        private String path;
        private String def;
        private static YamlConfiguration LANG;
    
        /**
         * Lang enum constructor.
         *
         * @param path
         *            The string path.
         * @param start
         *            The default string.
         **/
    
        Lang(String path, String start) {
            this.path = path;
            this.def = start;
        }
    
        /**
         * Set the {@code YamlConfiguration} to use.
         *
         * @param config
         *            The config to set.
         **/
    
        public static void setFile(YamlConfiguration config) {
            LANG = config;
        }
    
        @Override
        public String toString() {
            if (this == PREFIX)
                return ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def)) + " ";
            return ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def));
        }
    
        /**
         * Get the default value of the path.
         *
         * @return The default value of the path.
         **/
    
        public String getDefault() {
            return this.def;
        }
    
        /**
         * Get the path to the string.
         *
         * @return The path to the string.
         **/
    
        public String getPath() {
            return this.path;
        }
    }


    This is how I defined my prefix string:
    Code:
    String prefix = ChatColor.translateAlternateColorCodes('&', Lang.PREFIX.toString());
    This is the error that is given in the console:
    Console error (open)

    [09:42:29] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ws' in plugin WorldSpawns v2.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:629) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1295) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1155) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:695) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
    at me.greenadine.worldspawns.Lang.toString(Lang.java:96) ~[?:?]
    at me.greenadine.worldspawns.commands.CommandWorldspawns.onCommand(CommandWorldspawns.java:30) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
    ... 15 more



    I have never had this before. I have used a lang file before successfuly, without any issues. I have no clue to what I have to do to fix this. Could someone please help me?
     
  2. Offline

    Zombie_Striker

    @Kevinzuman22
    Are you sure the strings exist in the config? Try printing out if the strings exist or if the path in the config exists.
     
  3. Sorry for the late reply.

    The string indeed didn't exist. Very stupid error.

    Thanks for the help!
     
  4. Offline

    Zombie_Striker

    @Kevinzuman22
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page