Solved NullPointerException :/

Discussion in 'Plugin Development' started by Urag, Jul 23, 2017.

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

    Urag

    Why is sth null, help me :)

    When "/rb place"

    Show Spoiler
    Code:
    [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rb' in plugin RestoringBlocks v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1385) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1220) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_102]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_102]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_102]
    Caused by: java.lang.NullPointerException
        at me.miloszgnk.RestoringBlocks.Main.getRestoring_Blocks_Placing_Activated(Main.java:37) ~[?:?]
        at me.miloszgnk.RestoringBlocks.myCommandExecutor.onCommand(myCommandExecutor.java:41) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        ... 15 more


    Main:

    Show Spoiler
    Code:
    public class Main extends JavaPlugin {
       
        private static Main instance;
       
        public static Main getInstance() {
            return instance;
        }
       
        public List<Location> locations;
       
        public List<Location> getLocations(){
            return (List<Location>) getConfig().getConfigurationSection("locations");
        }
    
       
        HashMap<Player, Boolean> blockplacingactiv = new HashMap<Player, Boolean>();
        HashMap<Player, Boolean> blockbreakingactiv = new HashMap<Player, Boolean>();
       
        public void setRestoring_Blocks_Placing_Activated(Player p, Boolean b){
            blockplacingactiv.put(p, b);
            return;
        }
       
        public Boolean getRestoring_Blocks_Placing_Activated(Player p){
            if(blockplacingactiv.get(p == null)){
                blockplacingactiv.put(p, false);
            }
            return blockplacingactiv.get(p);
        }
       
        public void setRestoring_Blocks_Breaking_Activated(Player p, Boolean b){
            blockbreakingactiv.put(p, b);
        }
       
        public Boolean getRestoring_Blocks_Breaking_Activated(Player p){
            if(blockbreakingactiv.get(p == null)){
                blockbreakingactiv.put(p, false);
            }
            return blockbreakingactiv.get(p);
        }
    
       
       
        @Override
        public void onEnable(){
            instance=this;
            getCommand("rb").setExecutor(new myCommandExecutor());
            Bukkit.getServer().getPluginManager().registerEvents(new myEventExecutor(), this);
        }
       
        @Override
        public void onDisable(){
           
        }
    
    }


    myCommandExecutor:

    Show Spoiler
    Code:
    public class myCommandExecutor implements CommandExecutor {
    
        private Main main = Main.getInstance();
       
       
        List<Location>    locations = main.getLocations();
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("rb")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("[RestoringBlocks] You can only use that command in-game");
                }
                else{
                    sender.sendMessage("====[RestoringBlocks]===");
                    sender.sendMessage("/rb - shows menu");
                    sender.sendMessage("/rb place - activates placing restoring blocks");
                    sender.sendMessage("/rb break - activates removing restoring blocks");
                    sender.sendMessage("/rb reset - removes all restoring block");
                    sender.sendMessage("/rb tool - gives tool for editing restoring blocks");
                    sender.sendMessage("=========================");
                }
                if (args.length==1 && args[0].equalsIgnoreCase("place")){
                    Player p;
                    p = (Player) sender;
                    if(main.getRestoring_Blocks_Placing_Activated(p)==false){
                        main.setRestoring_Blocks_Placing_Activated(p, true);
                    }
                    else{
                        main.setRestoring_Blocks_Placing_Activated(p, false);
                    }
                    }
                }
               
                if (args.length==1 && args[0].equalsIgnoreCase("break")){
                    Player p;
                    p = (Player) sender;
                    if(main.getRestoring_Blocks_Breaking_Activated(p)==false){
                        main.setRestoring_Blocks_Breaking_Activated(p, true);
                    }
                    else{
                        main.setRestoring_Blocks_Breaking_Activated(p, false);
                    }
                }
               
                if (args.length==1 && args[0].equalsIgnoreCase("reset")){
                    locations.clear();
                    main.saveConfig();
                    main.reloadConfig();
                }
               
                if (args.length==1 && args[0].equalsIgnoreCase("tool")){
                    ItemStack item = new ItemStack(Material.FEATHER, 1);
                    ItemMeta im = item.getItemMeta();
                    im.setDisplayName("Restoring Blocks Tool");
                    item.setItemMeta(im);
                    ((HumanEntity) sender).getInventory().addItem(item);
                }
               
                //if (args.length==1 && !(args[0]).equalsIgnoreCase("break") || !(args[0]).equalsIgnoreCase("place") || !(args[0]).equalsIgnoreCase("reset") || !(args[0]).equalsIgnoreCase("tool")){
                    //p.sendMessage("[RestoringBlocks] Type \"rb\" for help");
                //}
               
               
               
         return true;
        }
       
    }
     
  2. Offline

    Machine Maker

    @Urag
    The two hashmaps<Player, Boolean> that you create in your Main class are never instantiated. try initializing the hashmaps outside of the onenable method and then setting them to the new HashMaps inside the on enable
    Code:
    HashMap<Player, Boolean> hashMap1;
    HashMap<Player, Boolean> hashMap2;
    
    public void onEnable() {
            //other stuff
            hashMap1 = new HashMap<Player, Boolean>;
            hashMap2 = new HashMap<Player, Boolean>;
    }
     
  3. Offline

    Caderape2

    @Urag cuz you are trying to get a null value from the hasmap.
    In the condition you say, p == null so it try to return null
     
  4. Offline

    Urag

    upload_2017-7-23_15-12-14.png
    upload_2017-7-23_15-11-26.png
     
  5. Offline

    Machine Maker

    @Urag oops, I forgot the "()" that go after the new HashMap<Player, Boolean>();
     
  6. Offline

    Urag

    And i had no idea where to put that (), xd

    Still
    upload_2017-7-23_15-17-24.png
     
  7. Offline

    Machine Maker

    @Urag
    Are you creating variables with the same name somewhere else?
     
  8. Offline

    Urag

    @X1machinemaker1X

    Oh yes, i haven't deleted old ones >_<

    Again error appears
    Show Spoiler
    Code:
    [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rb' in plugin RestoringBlocks v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1385) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1220) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_102]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_102]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_102]
    Caused by: java.lang.NullPointerException
        at me.miloszgnk.RestoringBlocks.Main.getRestoring_Blocks_Placing_Activated(Main.java:37) ~[?:?]
        at me.miloszgnk.RestoringBlocks.myCommandExecutor.onCommand(myCommandExecutor.java:41) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        ... 15 more

    I haven't known how to set default value to "false" so i made
    Show Spoiler
    Code:
            if(blockplacingactiv.get(p == null)){
                blockplacingactiv.put(p, false);
            }

    In that method
    Show Spoiler
    Code:
        public Boolean getRestoring_Blocks_Placing_Activated(Player p){
            if(blockplacingactiv.get(p == null)){
                blockplacingactiv.put(p, false);
            }
            return blockplacingactiv.get(p);
        }

    Maybe that's wrong
     
  9. Offline

    Machine Maker

    @Urag
    try this:
    Code:
    if (!blockplacingactive.keySet().contains(p)) {
            blockplacingactive.put(p, false);
    }
    The #keySet() is a Set of all the keys in the hashmap (the players)
    Just check if the keySet doesnt not contain the player
     
  10. Offline

    Urag

    Ok, it looks like it works, but there are appearing errors with another code, i bet i'll arrive soon with new problems :)
    Solved
     
  11. Offline

    Machine Maker

    @Urag Glad I could help.
     
  12. Offline

    RcExtract

    Don't name types starting in lower case

    Sent from my LG-H860 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page