Getting A Null Pointer Exception when storing Locations in a File

Discussion in 'Plugin Development' started by blok601, Dec 20, 2015.

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

    blok601

    Hello, I am getting a Null Pointer Exception (From the stack trace of course) when I am trying to store locations in a file. Here is the code and error:

    Code:
        private void createArena(Player p, String name){
            double x = p.getLocation().getX();
            double y = p.getLocation().getY();
            double z = p.getLocation().getZ();
            float pitch = p.getLocation().getPitch();
            float yaw = p.getLocation().getYaw();
           
            arenas.set(name + ".x", x);
            arenas.set(name + ".y", y);
            arenas.set(name + ".z", z);
            arenas.set(name + ".pitch", pitch);
            arenas.set(name + ".yaw", yaw);
           
            p.sendMessage(ChatColor.AQUA + "Arena " + name + " was succesfully created!");
        }
    Code:
    [11:41:14] [Server thread/INFO]: blok601 issued server command: /duel create fds
    [11:41:14] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duel' in plugin Duel v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:672) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:628) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:536) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at me.blok601.duel.Main.createArena(Main.java:27) ~[?:?]
        at me.blok601.duel.Main.onCommand(Main.java:130) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        ... 15 more

    And When I run my command:
    Code:
    if(args.length == 2){
                        if(args[0].equalsIgnoreCase("create")){
                            createArena(p, args[1]);
                        }
                           
                    }
    I get the error. What would I be doing wrong here?
     
  2. Offline

    adam753

    Is arenas null? You probably forgot to create it, assuming it's a hashmap.
     
  3. Offline

    blok601

    It's a file.

    @adam753
     
    Last edited: Dec 20, 2015
  4. Offline

    adam753

    @blok601
    A Bukkit Yaml file? Or what class?
    Which line is line 27 of the file?
     
  5. Offline

    blok601

    Sorry, a yaml file.

    Line 27 is setting the x variable in the method.
    @adam753
     
  6. Offline

    adam753

    @blok601
    The only thing that can be null on that line is p. I can see you're passing in variable "p" from onCommand, so that must be null I guess?
     
  7. Offline

    blok601

    Well, I need the player in the method to get the location from.
    @adam753
     
  8. Offline

    adam753

    @blok601
    Yeah, but unless there's something I'm missing, the p variable in onCommand must be null. Meaning you forgot to set it by accident.
     
  9. Offline

    blok601

    That's strange, I set it at the beginning of my onCommand
     
  10. Offline

    adam753

    @blok601
    Just to humor me, can you post your onCommand method?
     
  11. Offline

    blok601

    Gladly..:

    Code:
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(cmd.getName().equalsIgnoreCase("duel")){
                if(!(sender instanceof Player)){
                    sender.sendMessage(ChatColor.RED + "Only players can run the duel command!");
                    return false;
                }else{
                    Player p = (Player) sender;
                    if(args.length == 0){
                        p.sendMessage(ChatColor.RED + "Please use the command like this! /duel <player>");
                        return false;
                    }
                    if(args.length == 1){
                        if(args[0].equalsIgnoreCase("help")){
                            p.sendMessage(ChatColor.GOLD + "Duel is a simple plugin!");
                            p.sendMessage(ChatColor.GOLD + "Use these commands:");
                            p.sendMessage(ChatColor.GOLD + "/Duel <player>");
                            return true;
                        }if(args[0].equals("admin")){
                            p.sendMessage(ChatColor.AQUA + "The following are the Duel admin commands");
                            p.sendMessage(ChatColor.AQUA + "/Duel create <arena name>");
                            p.sendMessage(ChatColor.AQUA + "/Duel setspawn1 <arena name>");
                            p.sendMessage(ChatColor.AQUA + "/Duel setspawn2 <arena name>");
                            return true;
                        }else{
                            Player target = Bukkit.getPlayer(args[0]);
                            if(target == null){
                                p.sendMessage(ChatColor.RED + "Couldn't find the specified player. Are they online?");
                            }else{
                                openKit(p);
                        }
    
                    }
                       
                    } if(args.length == 2){
                        if(args[0].equalsIgnoreCase("create")){
                            if(args[1] == null){
                                p.sendMessage(ChatColor.RED + "Please specify a name for your arena.");
                                return false;
                            }
                            createArena(p, args[1].toString());
                        }
                           
                    }
                }
            }
            return false;
           
        }
     
  12. What is line 130 in your class?
    That'd be createArena(p, args[1].toString()); I guess
     
    blok601 likes this.
  13. Offline

    adam753

    @blok601
    Okay, so the player clearly isn't null. But you're saying you're getting a NullPointerException on
    Code:
    double x = p.getLocation().getX();
    I don't see anything else that can be null there. Are you certain that's the correct code that matches up with the error you posted?
     
  14. Offline

    Zombie_Striker

    @adam753 @blok601
    Could it be that your code has updated? Maybe it's not that line. Can you post your current code and current error.
     
  15. @blok601
    Do you get an error for your other commands; "help" and "admin"?

    @blok601

    Is that a yes or a no? :confused:
    Can you try using else-if statements?
    What happens if you don't give it an arena name? - To trigger line 37 in your code block, does that work?

    EDIT: Oh, sorry. Just double posted... I don't think I can delete my own posts though, so just leaving it here. Sorry :/ (Also, not sure if blok would be notified if I just edited my post?)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 21, 2015
    blok601 likes this.
  16. Offline

    blok601

    No.

    Also, I comepletely removed my createArena method and I am still getting errors in the same place. I tried using else if statements, but that didn't help either.

    Here is my current code (Full Main Class):
    Code:
    package me.blok601.duel;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        private void createArena(Player p, String name) {
            Location l = p.getLocation();
            arenas.set(name + ".location", l);
           
    
            settings.saveArenas();
    
            p.sendMessage(ChatColor.AQUA + "Arena " + name
                    + " was succesfully created!");
        }
    
    
    
        SettingsManager settings = SettingsManager.getInstance();
        PluginManager pm = Bukkit.getServer().getPluginManager();
    
        FileConfiguration config = settings.getConfig();
        FileConfiguration arenas = settings.getArenas();
    
        @SuppressWarnings("deprecation")
        private void openKit(Player player) {
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GOLD
                    + "Select a kit");
    
            short id = 1;
            ItemStack gapple = new ItemStack(Material.getMaterial(322), 1, id);
            ItemStack archer = new ItemStack(Material.BOW, 1);
            ItemStack arrow = new ItemStack(Material.ARROW, 1);
    
            ItemMeta gMeta = gapple.getItemMeta();
            gMeta.setDisplayName(ChatColor.AQUA + "Gapple Kit");
    
            ItemMeta aMeta = archer.getItemMeta();
            aMeta.setDisplayName(ChatColor.GREEN + "Archer Kit");
    
            ItemMeta arMeta = arrow.getItemMeta();
            arMeta.setDisplayName(ChatColor.BLACK + "Arrow Kit");
    
            arrow.setItemMeta(arMeta);
            archer.setItemMeta(aMeta);
            gapple.setItemMeta(gMeta);
    
            inv.setItem(1, gapple);
            inv.setItem(2, archer);
            inv.setItem(3, arrow);
    
            player.openInventory(inv);
    
        }
    
        /*
         * Commands: /duel <player> *opens gui* *player clicks kit* sends message to
         * target *target clicks chat message* teleport players to arena ./Duel
         * <player> ./Duel help ./Duel setspawn1 <Arena Name> .Duel setspawn2 <Arena
         * Name>
         */
    
        @Override
        public void onEnable() {
            settings.setup(this);
            Bukkit.getServer().getConsoleSender()
                    .sendMessage(ChatColor.GOLD + "Duel has been enabled!");
            pm.registerEvents(new Listeners(), this);
        }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String args[]) {
            if (cmd.getName().equalsIgnoreCase("duel")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED
                            + "Only players can run the duel command!");
                    return false;
                } else {
                    Player p = (Player) sender;
                    if (args.length == 0) {
                        p.sendMessage(ChatColor.RED
                                + "Please use the command like this! /duel <player>");
                        return false;
                    }
                    if (args.length <= 1) {
                     if (args[0].equalsIgnoreCase("help")) {
                            p.sendMessage(ChatColor.GOLD
                                    + "Duel is a simple plugin!");
                            p.sendMessage(ChatColor.GOLD + "Use these commands:");
                            p.sendMessage(ChatColor.GOLD + "/Duel <player>");
                            return true;
                       
                     } else if (args[0].equals("admin")) {
                            p.sendMessage(ChatColor.AQUA
                                    + "The following are the Duel admin commands");
                            p.sendMessage(ChatColor.AQUA
                                    + "/Duel create <arena name>");
                            p.sendMessage(ChatColor.AQUA
                                    + "/Duel setspawn1 <arena name>");
                            p.sendMessage(ChatColor.AQUA
                                    + "/Duel setspawn2 <arena name>");
                            return true;
                        } else {
                            Player target = Bukkit.getPlayer(args[0]);
                            if (target == null) {
                                p.sendMessage(ChatColor.RED
                                        + "Couldn't find the specified player. Are they online?");
                            } else {
                                openKit(p);
                            }
    
                        }
    
                    }else if(args.length == 2){
                        if(args[0].equals("create")){
                            p.sendMessage(ChatColor.GREEN + "Working so far");
                        }
                    }
                }
            }
            return false;
        }
    
    }
     
  17. @blok601
    And where do you get the error? Line 130
    Honestly, it looks like it should work. Try walking it through line by line, comment out code, etc..
    Also try logging the field that gives the error. So if it's p. Try logging it before you make a call to it, so you can see what it references.
     
    Last edited: Dec 21, 2015
  18. Offline

    adam753

    @blok601
    I also asked you to post the error that comes from your current code. Since line numbers can change, and it can send us on wild goose chases if you give us mismatched mode and errors.
     
    blok601 likes this.
  19. Offline

    blok601

    I fixed the code. I never found out the error, or what was causing it, but I reworked the code and it worked just fine.
     
  20. Offline

    87pen

    Mark as solved.
     
    Zombie_Striker likes this.
  21. Did you refactor it?
     
Thread Status:
Not open for further replies.

Share This Page