1.8.8 Null Pointer exceptions/Arrays out of bound

Discussion in 'Spigot Plugin Development' started by OctoberTroy, Apr 2, 2020.

  1. I know where the problem is in the code. Its the argument for the last one /kcc help 2. I just can't figure out why it spits these errors only when the base command is ran.

    My Code: (PLEASE NOTE I am aware my string and placeholderapis is nightmare type beat.)
    Code (YAML):
    public class KnightFallCMD1 implements CommandExecutor {

        public KnightFall plugin;

        public KnightFallCMD1(KnightFall plugin) {
            this.plugin = plugin;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("knightfall") || (cmd.getName().equalsIgnoreCase("kcc"))) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOCA1")));
                    return true;
                }
                // KnightFall Version
                Player p = (Player) sender;
                if (args.length == 0) {
                    if (p.hasPermission("kcc.knightfall")) {
                        for (String KnightFallLabel
    : plugin.getConfig().getStringList("Version")) {
                            String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel.replace("%version%", plugin.getDescription().getVersion()));
                            String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel.replace("%player%", p.getName()));
                            String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                            p.sendMessage(withPlaceholdersSet);
                        }
                    } else {
                        p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                    }
                }
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("help")) {
                        if (p.hasPermission("kcc.knightfall.help")) {
                            for (String KnightFallLabel2
    : plugin.getConfig().getStringList("Help")) {
                                String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel2.replace("%knightfall_version%", plugin.getDescription().getVersion()));
                                String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel2.replace("%player%", p.getName()));
                                String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                                p.sendMessage(withPlaceholdersSet);
                            }
                        } else {
                            p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                        }
                    }
                }
                    else if (args[1].equalsIgnoreCase("2")) {
                            if (p.hasPermission("kcc.knightfall.help")) {
                                for (String KnightFallLabel3
    : plugin.getConfig().getStringList("Help2")) {
                                    String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel3.replace("%knightfall_version%", plugin.getDescription().getVersion()));
                                    String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel3.replace("%player%", p.getName()));
                                    String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                                    p.sendMessage(withPlaceholdersSet);
                                }
                            } else {
                                p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                            }
                        }
                    }
                return true;
            }
        }

    Here is the error:

    Code (Text):

    [18:01:11 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'kcc' in plugin KnightFall v2.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:143) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:667) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1194) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1006) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-TacoSpigot-fd3209f3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:776) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:715) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:618) [spigot.jar:git-TacoSpigot-fd3209f3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at berserk.octobertroy.net.Commands.KnightFallCMD1.onCommand(KnightFallCMD1.java:67) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            ... 15 more
     
     
  2. You’re getting the second argument without even checking if args.length == 2.
     
  3. well args[1] and .length == 1 are not the same you should be checking if args.length == 2 instead of doing args.length == 1 else args[1] assuming that the arguments are 3 arguments long since length counts it like 0, 1, 2, 3 and arguments counts it as 1, 2, 3, 4 where arg[0] would equal length == 1 and so on.
     
  4. Thank you
    It works now however my no perm message doesn;t work. So basically if I do /knightfall everything works okay command is returned if i have no perm turns no perm. However for help, help 2 and reload it displays no perm with the messages even if I dont have the perms.

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("knightfall") || (cmd.getName().equalsIgnoreCase("kac"))) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOCA1")));
                    return true;
                }
                // KnightFall Version
                Player p = (Player) sender;
                if (args.length == 0) {
                    if (p.hasPermission("kac.knightfall")) {
                        for (String KnightFallLabel : plugin.getConfig().getStringList("Version")) {
                            String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel.replace("%version%", plugin.getDescription().getVersion()));
                            String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel.replace("%player%", p.getName()));
                            String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                            p.sendMessage(withPlaceholdersSet);
                        }
                    } else {
                        p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                    }
                }
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("help")) {
                        if (p.hasPermission("kac.knightfall.help")) {
                            for (String KnightFallLabel2 : plugin.getConfig().getStringList("Help")) {
                                String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel2.replace("%knightfall_version%", plugin.getDescription().getVersion()));
                                String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel2.replace("%player%", p.getName()));
                                String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                                p.sendMessage(withPlaceholdersSet);
                            }
                        } else {
                            p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                        }
                    }
                }
                if (args.length == 2) {
                    if (args[1].equalsIgnoreCase("2")) {
                        if (p.hasPermission("kac.knightfall.help")) {
                            for (String KnightFallLabel3 : plugin.getConfig().getStringList("Help2")) {
                                String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel3.replace("%knightfall_version%", plugin.getDescription().getVersion()));
                                String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel3.replace("%player%", p.getName()));
                                String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                                p.sendMessage(withPlaceholdersSet);
                            }

                        }
                        } else {
                            p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                        }
                    }
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("reload")) {
                        if (p.hasPermission("kac.admin.reload")) {
                            List<String> broadcast1 = plugin.getConfig().getStringList("Reload");
                                for (String line : broadcast1) {
                                    Bukkit.broadcastMessage(ColorUtils.color(line));
                                }
                                    p.sendMessage(ColorUtils.color(plugin.getConfig().getString("KacReload")));
                                    plugin.saveConfig();
                                    plugin.reloadConfig();
                                    p.sendMessage(ColorUtils.color(plugin.getConfig().getString("KacReloaded")));
                                }
                        }
                    else {
                        p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                    }
                    }
                }
                return true;
            }
        }
     
  5. are you opped? opped players inherit all permissions.
     
  6. You are doing your if statements wrong, you need to use else if after ``if (args.length...)...`` because otherwise each of them will run and you don't want that
     
  7. no lol. the statement will be checked, but only one of them can be true. its certainly SLIGHTLY more effective to use else ifs, but it wont run the code inside of it if its not true.
     
  8. Well it looks like to me because of this:

    Code (Text):
                if (args.length == 2) {
                    if (args[1].equalsIgnoreCase("2")) {
                        if (p.hasPermission("kac.knightfall.help")) {
                            for (String KnightFallLabel3 : plugin.getConfig().getStringList("Help2")) {
                                String withoutPlaceHoldersSet = ColorUtils.color(KnightFallLabel3.replace("%knightfall_version%", plugin.getDescription().getVersion()));
                                String withoutPlaceholdersSet = ColorUtils.color(KnightFallLabel3.replace("%player%", p.getName()));
                                String withPlaceholdersSet = PlaceholderAPI.setPlaceholders(p, withoutPlaceholdersSet);
                                p.sendMessage(withPlaceholdersSet);
                            }

                        }
                        } else {
                            p.sendMessage(ColorUtils.color(plugin.getConfig().getString("NOPERM1")));
                        }
                    }
    Your brackets are misplaced and you're doing basically:
    if (args[1].equalsIgnoreCase("2")) {
    show 2nd help page
    } else {
    send no permission code
    }

    where it should be one bracket before that so it matches up with the if (p.hasPermission("kac.knightfall.help")) { check.

    It also seems from your code that as long as "2" is the 2nd argument it won't matter it will always send that code if the permission matches up so whether you type "knightfall help 2" or "knightfall lol 2" it would always return true for that since your args[0].equals("help") check is in it's own area.