Solved NullPointerException?

Discussion in 'Plugin Development' started by WingedMLGPro, Jun 6, 2015.

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

    WingedMLGPro

    Hey All,
    Im creating a Regions plugin and for some reason it keeps on pulling a error of a null pointer exception!
    Here is the error:

    06.06 10:03:00 [Server] INFO ... 14 more
    06.06 10:03:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at me.WingedMLGPro.Commands.RegionCommand.onCommand(RegionCommand.java:65) ~[?:?]
    06.06 10:03:00 [Server] INFO Caused by: java.lang.NullPointerException
    06.06 10:03:00 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45]
    06.06 10:03:00 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-BT36_1.8_04.29.jar:git-Spigot-550ebac-7019900]
    06.06 10:03:00 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'region' in plugin SwiftyGuard v1.0
    06.06 10:03:00 [Server] ERROR null

    Here is my Main Class:
    Code:
    package me.WingedMLGPro;
    
    import me.WingedMLGPro.Commands.RegionCommand;
    import me.WingedMLGPro.Listeners.WandListener;
    import me.WingedMLGPro.Util.WandPoint;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyGuard was created by 35047
    * © at 6/06/15 at 12:53 PM
    */
    public class Regions extends JavaPlugin {
    
        public static String Error = "§c§l/§4§l/ §6";
        public static String CantFindPlayer = Error+"Sorry, but we could not find the player ";
        public static String NoCONSOLE = Error+"Sorry, But the Console can't run this command!";
        public static String TAG = "§6§lSwi§e§lfty§6§l>§e§l>§r ";
        public static String TAG2 = "§6§l/§e§l/ §6";
        public static String Arrows = "§e§l>§6§l> ";
        public static String SwiftyAnnounment = "§6§lSwi§e§lfty§6§l>§e§l>§r ";
        public static String PermCONSOLE = "§c§l/§4§l/ §cYou are not as cool as a Console, so stop trying to use this command!";
        public static String PermOWNER = "§c§l/§4§l/ §cYou are not as cool as a Owner, so stop trying to use this command!";
        public static String PermHEADADMIN = "§c§l/§4§l/ §cYou are not as cool as a HeadAdmin, so stop trying to use this command!";
        public static String PermAdmin = "§c§l/§4§l/ §cYou are not as cool as a Admin, so stop trying to use this command!";
        public static String PermMOD = "§c§l/§4§l/ §cYou are not as cool as a Mod, so stop trying to use this command!";
        public static String PermVIP = "§c§l/§4§l/ §cYou are not as cool as a VIP, so stop trying to use this command!";
        public static String PermHELPER = "§c§l/§4§l/ §cYou are not as cool as a Helper, so stop trying to use this command!";
        public static String PermIMMORTAL = "§c§l/§4§l/ §cYou are not as cool as a Immortal, so stop trying to use this command!";
        public static String PermLEGEND = "§c§l/§4§l/ §cYou are not as cool as a Legend, so stop trying to use this command!";
        public static String PermELITE = "§c§l/§4§l/ §cYou are not as cool as a Elite, so stop trying to use this command!";
        public static String PermHERO = "§c§l/§4§l/ §cYou are not as cool as a Hero, so stop trying to use this command!";
        public static String PermSergeant = "§c§l/§4§l/ §cYou are not as cool as a Sergeant, so stop trying to use this command!";
    
        private static Map<String, WandPoint> wandStorage = new HashMap<String, WandPoint>();
    
        public static Map<String, WandPoint> getWandStorage() {
            return wandStorage;
        }
    
        public static void setWandStorage(Map<String, WandPoint> wandStorage) {
            Regions.wandStorage = wandStorage;
        }
    
        private static Regions instance;
    
        public static Regions getInstance() {
            return instance;
        }
    
        @Override
        public void onEnable(){
    
            Bukkit.getPluginManager().registerEvents(new WandListener(), this);
    
            getCommand("region").setExecutor(new RegionCommand());
    
            getLogger().info("SwiftyGuard has been enabled Version: "+getDescription().getVersion());
    
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
    }
    
    And Here is my RegionsCommand Class:

    Code:
    package me.WingedMLGPro.Commands;
    
    import me.WingedMLGPro.Regions;
    import me.WingedMLGPro.Util.WandPoint;
    import me.dylzqn.PlayerAPI.SwiftyPlayer;
    import me.dylzqn.Utils.RankManager;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    
    /**
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyGuard was created by 35047
    * © at 6/06/15 at 1:02 PM
    */
    public class RegionCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player p = (Player)sender;
            SwiftyPlayer user = new SwiftyPlayer(p);
    
            if (cmd.getName().equalsIgnoreCase("region")){
                if (!(user.getRank().equals(RankManager.HEADADMIN) || user.getRank().equals(RankManager.OWNER))){
                    user.sendMessage(Regions.PermHEADADMIN);
                    return true;
                }
                if (args.length==0){
                    p.sendMessage(Regions.Arrows+"§bRegion Command§7:");
                    p.sendMessage(Regions.Arrows+"§b/region create §a<RegionName>");
                    p.sendMessage(Regions.Arrows+"§b/region delete §a<RegionName>");
                    p.sendMessage(Regions.Arrows+"§b/region list"); //TODO figure out how to
                    p.sendMessage(Regions.Arrows+"§b/region tp §a<RegionName>");
                    p.sendMessage(Regions.Arrows+"§b/region wand");
                    p.sendMessage(Regions.Arrows+"§b/region setPvP §a<RegionName> <True|False>");
                    p.sendMessage(Regions.Arrows+"§b/region setEnterMessage §a<RegionName> <Message>");
                    p.sendMessage(Regions.Arrows+"§b/region setLeaveMessage §a<RegionName> <Message>");
                    return true;
                }
                String subCmd = args[0];
                if (subCmd.equalsIgnoreCase("create")){
                    if (args.length<=1){
                        p.sendMessage(Regions.Error+"Usage: §b/region create §a<RegionName>");
                        return true;
                    }
                    if (!Regions.getWandStorage().containsKey(p.getName())){
                        p.sendMessage(Regions.Error+"You haven't selected an area of where the Region will go!\n"+Regions.Arrows+"§bDo /region wand§7: §ato be able to select the area");
                        return true;
                    }
                    WandPoint wand = Regions.getWandStorage().get(p.getName());
                    if(wand.getP1()!=null&&wand.getP2()!=null) {
                        if (wand.getP1().getWorld().getName().equals(wand.getP2().getWorld().getName())) {
                            String rgName = args[1];
                            if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)!=null){
                                p.sendMessage(Regions.Error+"The Region with the name "+rgName+" already exists!");
                                return true;
                            }
                            else if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null) {
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".Creator", p.getName());
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".Location 1", wand.getP1());
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".Location 2", wand.getP2());
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".PvP", "false");
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".EnterMsg", "null");
                                Regions.getInstance().getConfig().set("SwiftyGuard." + rgName + ".LeaveMsg", "null");
                                Regions.getInstance().saveConfig();
                                p.sendMessage(Regions.TAG2 + "You created a new region with the name " + rgName);
                            }
                        }
                        else {
                            p.sendMessage(Regions.Error+"You have to select an area that is in the same world!");
                        }
                        return true;
                    }
                }
                if (subCmd.equalsIgnoreCase("delete")){
                    if (args.length<=1){
                        p.sendMessage(Regions.Error+"Usage: §b/region delete §a<RegionName>");
                        return true;
                    }
                    String rgName = args[1];
                    if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null){
                        p.sendMessage(Regions.Error+"There is no Region with the name "+rgName);
                        return true;
                    }
                    else {
                        Regions.getInstance().getConfig().set("SwiftyGuard."+rgName, null);
                        Regions.getInstance().saveConfig();
                        p.sendMessage(Regions.TAG2+"You deleted the Region "+rgName);
                    }
                }
                if (subCmd.equalsIgnoreCase("tp")){
                    if (args.length<=1){
                        p.sendMessage(Regions.Error+"Usage: §b/region tp §a<RegionName>");
                        return true;
                    }
                    String rgName = args[1];
                    if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null){
                        p.sendMessage(Regions.Error+"There is no Region with the name "+rgName);
                        return true;
                    }
                    Location l1 = (Location) Regions.getInstance().getConfig().get("SwiftyGuard."+rgName+".Location 1");
                    p.teleport(l1);
                    p.sendMessage(Regions.TAG2+"Ypu teleported to the region "+rgName);
                }
                if (subCmd.equalsIgnoreCase("wand")){
                    PlayerInventory pi = p.getInventory();
                    ItemStack wand = new ItemStack(Material.STONE_AXE);
                    ItemMeta meta = wand.getItemMeta();
                    meta.setDisplayName("§e§lSwi§6fty §e§lWa§6§lnd");
                    wand.setItemMeta(meta);
                    pi.addItem(wand);
                    p.sendMessage(Regions.TAG2 + "You got the want item!");
                    p.sendMessage(Regions.Arrows+"§bLeft Click Block§7: §aTo Select Location 1 of your Region!");
                    p.sendMessage(Regions.Arrows+"§bRight Click Block§7: §aTo Select Location 2 of your Region!");
                }
                if (subCmd.equalsIgnoreCase("setPvP")){
                    if (args.length<=2 ){
                        p.sendMessage(Regions.Error+"Usage: §b/region setPvp §a<RegionName> <True|False>");
                        return true;
                    }
                    String rgName = args[1];
                    String PvP = args[2];
                    if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null){
                        p.sendMessage(Regions.Error+"There is no Region with the name "+rgName);
                        return true;
                    }
                    if (PvP.equalsIgnoreCase("true")){
                        Regions.getInstance().getConfig().set("SwiftyGuard."+rgName+".PvP", "true");
                        Regions.getInstance().saveConfig();
                        p.sendMessage(Regions.TAG2+"You set PvP in the region "+rgName+" to "+PvP);
                        return true;
                    }
                    if (PvP.equalsIgnoreCase("false")){
                        Regions.getInstance().getConfig().set("SwiftyGuard."+rgName+".PvP", "false");
                        Regions.getInstance().saveConfig();
                        p.sendMessage(Regions.TAG2+"You set PvP in the region "+rgName+" to "+PvP);
                        return true;
                    }
                    else {
                        p.sendMessage(Regions.Error+"You can only set PvP to be False or True!");
                        return true;
                    }
                }
                if (subCmd.equalsIgnoreCase("setEnterMessage")){
                    if (args.length<=2){
                        p.sendMessage(Regions.Error+"Usage: §b/region setEnterMessage §a<RegionName> <Message>");
                        return true;
                    }
                    String rgName = args[1];
                    String msg = "";
                    for (int i = 2; i < args.length; i++){
                        msg += args[i] + " ";
                    }
                    msg = msg.replaceAll("&", "§");
                    if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null){
                        p.sendMessage(Regions.Error+"There is no Region with the name "+rgName);
                        return true;
                    }
                    else {
                        Regions.getInstance().getConfig().set("SwiftyGuard."+rgName+".EnterMsg", msg);
                        Regions.getInstance().saveConfig();
                        p.sendMessage(Regions.TAG2+"You set the Enter Message for the Region "+rgName+" to "+msg);
                    }
                }
                if (subCmd.equalsIgnoreCase("setLeaveMessage")){
                    if (args.length<=2){
                        p.sendMessage(Regions.Error+"Usage: §b/region setLeaveMessage §a<RegionName> <Message>");
                        return true;
                    }
                    String rgName = args[1];
                    String msg = "";
                    for (int i = 2; i < args.length; i++){
                        msg += args[i] + " ";
                    }
                    msg = msg.replaceAll("&", "§");
                    if (Regions.getInstance().getConfig().get("SwiftyGuard."+rgName)==null){
                        p.sendMessage(Regions.Error+"There is no Region with the name "+rgName);
                        return true;
                    }
                    else {
                        Regions.getInstance().getConfig().set("SwiftyGuard."+rgName+".LeaveMsg", msg);
                        Regions.getInstance().saveConfig();
                        p.sendMessage(Regions.TAG2+"You set the Leave Message for the Region "+rgName+" to "+msg);
                    }
                }
            }
            return true;
        }
    }
    
    Thanks!

    -WingedMLGPro
     
  2. Offline

    I Al Istannen

    @WingedMLGPro You never set the instance. You need to do so in your "onEnable()".

    And don't log enable or disable messages. Bukkit does that for you.
    Consider not using "§" but "ChatColor.translateAlternateColorCodes" as that can change in the future.

    You can also use "saveDefaultConfig()" instead of "config.copyDefaults()" and "config.save()". This will preserve the comments.
     
  3. Offline

    caderape

    @WingedMLGPro get a look to constructor please.
    Show the full error log
     
  4. Offline

    WingedMLGPro

Thread Status:
Not open for further replies.

Share This Page