Formatting Problem

Discussion in 'Spigot Plugin Development' started by MaxNatural, Jul 2, 2016.

Thread Status:
Not open for further replies.
  1. I have this code and everything is working, I just need help formatting everything because I get null pointer exceptions.

    Code (Text):
    package kits;

    import java.util.HashMap;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;

    public class Bounty implements CommandExecutor, Listener {

        public Map<String, Integer> bountyMap = new HashMap<String, Integer>();

        public Bounty(Main main) {
            // TODO Auto-generated constructor stub

        public boolean onCommand(CommandSender cs, Command c, String l, String[] args) {
            Player p = (Player) cs;
            Player bounty = Bukkit.getServer().getPlayer(args[0]);
            int min = 100;
            boolean bounties = true;
            int sayd = Integer.parseInt(args[1]);
            int say = (int) Math.ceil(sayd);
            double bal = Stats.getCoins(p.getName());
            if (p.hasPermission("lit.bounty")) {
                if (bounties == true) {
                    if (say > min) {
                        if (bal > say) {
                            if (bounty.isOnline()) {
                                Bukkit.broadcastMessage(ChatColor.RED + "" + ChatColor.BOLD + say + ChatColor.GREEN + "" + ChatColor.BOLD + " bounty was placed on " + ChatColor.RED + ChatColor.BOLD + bounty.getName());;
                                Stats.takeCoins(p.getName(), say);
                                bounty.sendMessage(Main.main + ChatColor.RED + "You have a bounty on you! Avoid ALL players.");
                                bountyMap.put(bounty.getName(), say);
                                if (args.length != 3) {
                                    p.sendMessage(Main.main + "Incorrect Syntax, Use: /bounty (Player) (Ammount)");
                                    return false;
                            } else {
                                cs.sendMessage(Main.main + "Player is offline.");
                                return false;
                        } else {
                            cs.sendMessage(Main.main + "You don't have enough coins!");
                            return false;
                    } else {
                        cs.sendMessage(Main.main + "You must bounty more than" + min + "!");
                        return false;
                } else {
                    cs.sendMessage(Main.main + "Bounties are disabled on this server!");
                    return false;
            } else {
                return false;
            return true;
        public void onDeath(PlayerDeathEvent e) {
            Entity dead = e.getEntity();
            Player p = ((Player) dead).getPlayer();
            Player k = ((Player) dead).getKiller();
            if (bountyMap.containsKey(p.getName())) {
                Stats.addCoins(p.getName(), bountyMap.get(k.getName()));
                Bukkit.getServer().broadcastMessage(ChatColor.RED + "" + ChatColor.BOLD + k.getName() + ChatColor.GREEN + "" + ChatColor.BOLD + " has killed " + ChatColor.RED + "" + ChatColor.BOLD + p.getName() + ChatColor.GREEN + "" + ChatColor.BOLD + " for a " + ChatColor.RED + "" + ChatColor.BOLD + bountyMap.get(p.getName()));
    Here is the console code.

    Code (Text):
    21:46:03 INFO]: Macks issued server command: /bounty
    [21:46:03 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bounty' in plugin Kitsv0.0.1
            at org.bukkit.command.PluginCommand.execute( ~[craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at org.bukkit.command.SimpleCommandMap.dispatch( ~[craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand( ~[craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand( [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a( [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$ [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_92]
            at Source) [?:1.8.0_92]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D( [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D( [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C( [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at [craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            at Source) [?:1.8.0_92]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at ~[?:?]
            at org.bukkit.command.PluginCommand.execute( ~[craftbukkit-1.9.jar:git-Bukkit-7fc5cd8]
            ... 15 more

  2. Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at ~[?:?]

    Player bounty = Bukkit.getServer().getPlayer(args[0]);

    args[0] does not exist because the args array is empty.
    • Useful Useful x 1
  3. How would I add it? Or another way to do it?
  4. Learn basic java, for a start.

    [spoonfeed] To fix this, just do a check if(args.length < 1) return false; [/spoonfeed]
  5. Wrap the command section in a try-catch to idiot proof it from throwing nasty errors when a user
    of the software either accidentally or deliberately leaves out the player who will be targeted as the
    next bounty.

    There's no need to be nasty to someone when they ask for help unless
    you see them all over the forums constantly asking questions that could be easily
    answered with a little research.

    A question here and there is harmless, so please treat it as such. :)
  6. Yet this can be answered with little research? This is basic Java here.
  7. Correct, and everyone has to start somewhere.
    Even you and I didn't understand basic Java at one point.
  8. FlyingLlama is giving very good advice that the OP should defiantly be taking. You shouldn't be programming a plugin if you don't know the very very basics such as reading errors.
  9. Exactly, everyone has to start somewhere. So start by reading Java programming guides instead of diving right into it.
  10. Indeed
Thread Status:
Not open for further replies.