Null in command

Discussion in 'Plugin Development' started by Marosking, Aug 23, 2016.

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

    Marosking

    Hello guys!

    Hello people! He writes plug , but unfortunately it throws me an error after entering the command.

    Here you have command code:
    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.marosking.castlemod.main.CastlePlugin;
    
    public class CreateCommand implements CommandExecutor {
    
        CastlePlugin plugin;
    
        public CreateCommand(CastlePlugin plugin) {
            this.plugin = plugin;
            this.plugin.getCommand("create").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
                throws NullPointerException {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Musisz byc graczem, aby uzyc tej komendy!");
                return true;
            }
    
            Player player = (Player) sender;
    
            if ((!player.hasPermission("castle.commands.create")) && (!player.hasPermission("castle.admin"))) {
                player.sendMessage(this.plugin.fixColor("&4Blaod: &cNie masz uprawnien do wykonania tej komendy!"));
                return true;
            }
    
            if (args.length < 3 || args.length > 4) {
                player.sendMessage(
                        this.plugin.fixColor("&4Blad: &cPoprawne uzycie: /create <arena> <minPlayers> <maxPlayers>"));
                return true;
            }
    
            String arenaName = args[0];
    
            if (!arenaName.matches("[a-zA-Z]+")) {
                player.sendMessage(this.plugin.fixColor("&4Blad: &cNazwa areny moze skladac sie tylko z liter!"));
                return true;
            }
    
            int minPlayers = Integer.parseInt(args[1]);
            int maxPlayers = Integer.parseInt(args[2]);
    
            this.plugin.castleArenaManager.getManager().createArena(arenaName, minPlayers, maxPlayers);
            player.sendMessage(this.plugin.fixColor("&cArena o nazwie &4" + arenaName + " &czostala poprawnie utworzona!"));
            return true;
        }
    
    }
    
    Here you also have the functions of the creation of the arena:
    Code:
    public void createArena(String arenaName, int minPlayers, int maxPlayers) {
            CastleArena castleArena = new CastleArena(arenaName, minPlayers, maxPlayers);
    
            this.plugin.getConfigFile().set("arenas." + arenaName, null);
            this.plugin.getConfigFile().set("arenas." + arenaName + ".minPlayers", minPlayers);
            this.plugin.getConfigFile().set("arenas." + arenaName + ".maxPlayers", maxPlayers);
            this.plugin.saveConfig();
    
            this.castleArenas.put(arenaName, castleArena);
        }

    And here you have code errors how when you type the command:
    Code:
    [06:02:46] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stworz' in plugin CastleMod v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_101]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_101]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [Spigot.jar:git-Spigot-044d928-e8c6403]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at com.marosking.castlemod.main.commands.CreateCommand.onCommand(CreateCommand.java:50) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-044d928-e8c6403]
        ... 15 more

    I really do not know what this can be null , please help!
    P.S. Sorry for my English , but I'm Polish
     
  2. Online

    timtower Administrator Administrator Moderator

    Moved to plugin development
    @Marosking What is line 50 of the createcommand class? I think that the arenamanager is null
     
  3. Offline

    Marosking

    @timtower
    50 line is: this.plugin.castleArenaManager.getManager().createArena(arenaName, minPlayers, maxPlayers);
    which here can be null?
     
  4. Online

    timtower Administrator Administrator Moderator

    @Marosking castleArenaManager, result of getManager.
    Try and find out.
     
  5. Offline

    HeartandSoul

    What do you mean by "He writes plug"? Did you copy some of this code?

    As @timtower mentioned, arena manager is null. This isn't your main class. Create a constructor to your main class, and use 'plugin' instead of 'this.plugin'. If it equals plugin, use it.

    EDIT: I'm looking at the line numbers in my IDE.
    If you have a good constructor, Then your class should take plugin.
    If you pasted your ENTIRE CreateCommand class, then the line numbers tell me that line 50 is a return statement. Something in that if statement is null, assuming it's:
    Code:
            this.plugin.castleArenaManager.getManager().createArena(arenaName, minPlayers, maxPlayers);
    
    It could be castleArenamanager, or your constructor returning plugin as null (this.plugin = plugin)
    Keep in mind that you don't need to use "this.plugin" if you have plugin equal "this.plugin".

    Paste all of your main class, along with whatever classes this code uses.

    EDIT 2: Also check for null on args[0]:
    Code:
            String arenaName = args[0];
    
            if (!arenaName.matches("[a-zA-Z]+")) {
                player.sendMessage(this.plugin.fixColor("&4Blad: &cNazwa areny moze skladac sie tylko z liter!"));
                return true;
            }
    EDIT 3: Don't blindly cast!!

    Code:
            Player player = (Player) sender;
     
    Last edited: Aug 23, 2016
  6. Offline

    InstanceofDeath

    You should always use try catch instead of giving the Exception the JavaPlugin class wtf

    And you forgot a bracket at the end of the onCommand method, didnt you?
     
  7. Offline

    HeartandSoul

    No, he put it after the 'throw NullPointerException' line.
    EDIT: I think so....

    He's right though ^. Don't do that.

    Code:
    try{
        this.whatever();
    } catch (Exception e) {
        e.printStackTrace();
    }
     
  8. Offline

    InstanceofDeath

    lol sry, I am blind af
     
Thread Status:
Not open for further replies.

Share This Page