Solved Problem with registering CommandExecutor

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

Thread Status:
Not open for further replies.
  1. Error:
    Code:
    java.lang.NullPointerException: null
        at me.miloszgnk.RestoringBlocks.myCommandExecutor.<init>(myCommandExecutor.java:20) ~[?:?]
        at me.miloszgnk.RestoringBlocks.Main.onEnable(Main.java:54) ~[?:?]
    myCommandExecutor class, line 20, i hope this has no connection with .-.
    Code:
    List<Location>    locations = main.getLocations();
    Main class, line 54
    Code:
    getCommand("rb").setExecutor(new myCommandExecutor());
     
  2. Offline

    Zombie_Striker

    @Urag
    main is null. You need to pass the main instance to the command executor before you can reference it.
     
    RcExtract likes this.
  3. 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");
        }
       
        public void setLocations(Location loc){
            getConfig().set("locations", loc);
        }
        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){
            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){
            return blockbreakingactiv.get(p);
        }
    
       
       
        @Override
        public void onEnable(){
            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){
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("rb")){
                if (args.length==0){
                    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")){
    
                   
                    if (!(sender instanceof Player)){
                        sender.sendMessage("[RestoringBlock] You can only use that command in-game.");
                    }
                    else 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")){
                    if (!(sender instanceof Player)){
                        sender.sendMessage("[RestoringBlock] You can only use that command in-game.");
                    }
                    else 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);
                    p.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;
        }
       
    }


    I don't understand, could you give more details? ;)
     
  4. Offline

    Caderape2

    @Urag
    You never initialize this variable.
     
  5. oh, yes.
    i made instance=this; in onEnable() and works, thanks ;)
     
Thread Status:
Not open for further replies.

Share This Page