java.lang.NullPointerException

Discussion in 'Plugin Development' started by tehchuanyeow, Jul 31, 2020 at 8:18 PM.

  1. Offline

    tehchuanyeow

    My console keeps throwing a NullPointerException ever since I seperated my Commands into their own Classes. I'm doing it abstract through a CommandManager & everything works but for some reason the commands keep throwing a NullPointerException. I don't think it's my Plugin.yml either cause its structured accordingly as well.

    CommandManager Class
    Code:
    package chuanyeow.Commands;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandManager implements CommandExecutor {
        private ArrayList<AbstractCommand> commands = new ArrayList<AbstractCommand>();
    
        public void setup() {
            commands.add(new forcestartCommand());
            commands.add(new hauntCommand());
            commands.add(new hubitemsCommand());
            commands.add(new manhuntlobbyCommand());
            commands.add(new menuCommand());
            commands.add(new serverhelpCommand());
            commands.add(new serverinfoCommand());
            commands.add(new spawnCommand());
        }
    
        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
            Player player = (Player) commandSender;
            AbstractCommand target = get(command.getName());
    
            if (target == null) {
                System.out.println(target);
                return true;
            }
           
            try {
                target.onCommand(player, strings);
            } catch (Exception e) {
                System.out.println(e);
            }
            return true;
        }
    
        private AbstractCommand get(String name) {
            for (AbstractCommand cmd : commands) {
                if (cmd.name().equalsIgnoreCase(name)) {
                    return cmd;
                }
            }
            return null;
        }
    }
    Abstract Command Class
    Code:
    package chuanyeow.Commands;
    
    import org.bukkit.entity.Player;
    
    public abstract class AbstractCommand {
        public abstract void onCommand(Player player, String[] args);
    
        public abstract String name();
    
        public abstract String info();
    }
    
    And in each Command Class is structured standard:
    SpawnCommand Class
    Code:
    public class spawnCommand extends AbstractCommand {
        private Final plugin;
    
        // Commands
        @Override
        public void onCommand(Player player, String[] args) {
            player.message("Ooga Booga!");
            return;
        }
       
        @Override
        public String name() {
            return "spawn";
        }
    
        @Override
        public String info() {
            return "Spawns the Player into the Hub";
        }
    The way I initialize it in the Final class is like so:
    Code:
    public void LoadCommands() {
            try {
                // Command Manager
                commandManager = new CommandManager();
                getCommandManager().setup();
                // Load Commands
                this.getCommand("forcestart").setExecutor(getCommandManager());
                this.getCommand("haunt").setExecutor(getCommandManager());
                this.getCommand("hubitems").setExecutor(getCommandManager());
                this.getCommand("manhuntlobby").setExecutor(getCommandManager());
                this.getCommand("menu").setExecutor(getCommandManager());
                this.getCommand("serverhelp").setExecutor(getCommandManager());
                this.getCommand("serverinfo").setExecutor(getCommandManager());
                this.getCommand("spawn").setExecutor(getCommandManager());
            } catch (Exception e) {
                System.out.println(e);
                return;
            }
            getServer().getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + "Commands Completed!");
        }
    I feel like the NullPointerException answer is staring me in the face but I really am stumped.
     
  2. Offline

    KarimAKL

Share This Page