Solved Error with null

Discussion in 'Plugin Development' started by ramptoetsenbord, Dec 8, 2017.

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

    ramptoetsenbord

    So, I am trying to make a /Message <player> <message> command so you can send private messages.
    but I always get this error:
    Code:
    [08:53:49 INFO]: ramptoetsenbord issued server command: /msg ramptoetsenbord myself
    [08:53:49 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'msg' in plugin BowPVPlugin vBeta0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1385) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1220) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
            at TeamCraft.bowPVP.Commands.Message.onCommand(Message.java:28) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            ... 15 more
    And here 's my code:
    Code:
    package TeamCraft.bowPVP.Commands;
    
    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.Player;
    
    public class Message implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender cmdsender, Command cmd, String cmdLabel, String[] args) {
    
            if  (args.length >= 2) {
       
                Player sender = (Player) cmdsender; // get player
                Player reciever = Bukkit.getServer().getPlayer(args[0]); // get player
       
                if (reciever != null) {
           
                    String message = "";
                    String arg = "";
                    int argP = 1;
    
                    for (int i = 1; i < args.length + 1; i++) {
    
                        arg = args[argP] + " "; // get the argument, and add a
                                                    // space
                                                    // so that the words get spaced
                                                    // out
                        message = message + arg; // add the argument to myString
                        argP += 1;
                    }
    
                    sender.sendMessage(ChatColor.DARK_AQUA + "Your message has been send."); //send message
                    reciever.sendMessage("[" + sender + " -> You]: " + message); //send message
                    return true;
                } else if (reciever == null) {
    
                    sender.sendMessage(ChatColor.DARK_RED + "Player " + args[0] + " can 't be found."); // send message
    
                }
    
                return false;
            } else {
       
                Player sender = (Player) cmdsender; // get player
                sender.sendMessage(ChatColor.RED + "Please use: /msg <player> <message>"); // send message
       
            }
            return false;
        }
    
    }
     
    Last edited: Dec 9, 2017
  2. Offline

    Zombie_Striker

    @ramptoetsenbord
    Args will never be null. You need to do args length checks before getting the first arg. Replace the if statement with....[coce=java] if (args.length >= 2){[/code] ...to make sure that there are atleast two args.
     
  3. Offline

    ramptoetsenbord

    I have added your tip but still get the same error.
    Its something with line 28 but i don 't know how to fix it.
     
  4. Offline

    Axe_Villager

    Hello ramptoetsenbord,

    I will do my best to assist you. Taken from the console snippet you provided, I take it you are attempting to perform the command:

    [08:53:49 INFO]: ramptoetsenbord issued server command: /msg ramptoetsenbord myself


    The command does not function as intended and it is caused by:

    java.lang.ArrayIndexOutOfBoundsException: 2
    at TeamCraft.bowPVP.Commands.Message.onCommand(Message.java:28) ~[?:?]



    Let's digest this information. By making a list of what we know it all becomes clearer.
    1. The exception occurs at line 28. in the Message class.
    2. The exception is of type ArrayIndexOutOfBoundsException.
    3. The number 2 is out of bounds.

    Now that we have made a list of clues that will help us, let's locate where the error is. With the information we have gathered, we can tell where and what the mistake is.

    The error occurs at line 28. in the Message class. That is specifically this line (I counted the lines manually, however, it would be helpful if you posted line numbers with your code in the future):

    Code:
    arg = args[argP] + " "; // get the argument, and add a

    To better understand this specific piece of code, let's bring the rest of the code you are using together with it. This will make it easier to understand what is going on without having to look back and forth at my reply and your original code snippet.

    Code:
    String message = "";
    String arg = "";
    int argP = 1;
    
    for (int i = 1; i < args.length + 1; i++) {
    
        arg = args[argP] + " "; // get the argument, and add a
                                // space
                                // so that the words get spaced
                                // out
        message = message + arg; // add the argument to myString
        argP += 1;
    }

    Now it is clear what the code on line 28. does. You are updating the variable arg with every argument after the first argument (the player's name) in a loop. This goes on until there are no arguments left. At least that is what you are trying to achieve.

    The exception that occur is of type ArrayIndexOutOfBoundsException. If you don't know what that is - I highly recommend you to read about it here: https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

    A short description, taken from the document, would be: Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

    Let's establish the information we know.
    1. The for loop's condition is true for as long as i < args.length + 1.
    2. When you perform the command: /msg ramptoetsenbord myself, the value of args.length is 2.
    3. The value of args.length + 1 is therefore equal to 2 + 1 = 3.
    4. Argument 0. is ramptoetsenbort, argument 1. is myself and argument 2. does not exist.

    Remember! Arrays in Java are zero-based. That means the first object in an array is at index 0, the second is at index 1. In this example, the size of the array is 2.

    Now the issue should become clear. In the loop, you are assigning the value of args[argP] to the variable arg for as long as i is less than 3. The code is working as intended up until the last iteration of the loop. You are attempting to use the value of args[argP] where argP = 2. Because the size of the array is only 2, the object args[2] does not exist.

    We can therefore conclude the code is not working because you are accessing the array with an illegal index. That index, to be specific, is 2. 2 is equal to the size of the array, hence why it does not work. To make the code work you will have to prevent the loop from attempting to access the array with an illegal index. This is easily done by changing the condition of the for loop to only return true if i is less than the size of the array.
     
    Last edited: Dec 9, 2017
  5. Offline

    ramptoetsenbord

     
  6. array indexing starts at 0
     
  7. Offline

    Shmunx

    This may be stupid but do you have essentials installed? If so then your command collides with essentials.
     
  8. Offline

    ramptoetsenbord

    first arg is a playername

    And, no i don 't have essentials installed

    But its already fixed, thx axe_villager!
     
Thread Status:
Not open for further replies.

Share This Page