Hot Summer Deals are Here!
Celebrate with up to 99% off on 17,100 resources
03
Days
11
Hours
25
Mins
57
Secs

Help - Anyone know why this is causing an error?

Status
This thread has been locked.

PurrfectMistake

¬ Owner of AetherTale.com
Premium
Feedback score
10
Posts
307
Reactions
95
Resources
0
Line 35 is causing the error.. (Player target = Bukkit.getServer().getPlayer(args[0]);

Unsure why, the code works but it throws out an error.

Code:
package me.nikk.Fess.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.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import me.nikk.Fess.utils.Configuration;

public class Ping
implements Listener, CommandExecutor{
   
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        @SuppressWarnings("unused")
        Player player = (Player)sender;
        if (cmd.getName().equalsIgnoreCase("ping")){
            if(sender instanceof Player)
            {
                Player p = (Player)sender;
                if(p.hasPermission("fess.ping"))
                {

                    int ping = ((CraftPlayer) sender).getHandle().ping;
                    p.sendMessage("");
                    p.sendMessage(Configuration.pluginprefix + ChatColor.GREEN + " Your ping is " + ChatColor.YELLOW + ping);
                    p.sendMessage("");
                    }
                if (args.length == 0);
                {
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null)
                    {
                      sender.sendMessage(Configuration.pluginprefix + " " + ChatColor.DARK_RED + "Player cannot be found!");
                    }
                    int tping = ((CraftPlayer) target).getHandle().ping;
                    p.sendMessage("");
                    p.sendMessage(Configuration.pluginprefix + ChatColor.YELLOW + " " + target.getName() + ChatColor.GREEN + "'s ping is " + ChatColor.YELLOW + tping);
                    p.sendMessage("");
                }
            }
                else
                {
                    sender.sendMessage(Configuration.pluginprefix + " " + Configuration.nopermission);
                }
            }
            else
            {
                sender.sendMessage(Configuration.pluginprefix + ChatColor.RED + "This is a ingame command! Join to use this.");
            }
        return false;
}
}
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Dori

Full Stack Developer
Supreme
Feedback score
28
Posts
459
Reactions
198
Resources
0
You're checking if args.length == 0. If there's args[0] args.length will be 1.
 

Dori

Full Stack Developer
Supreme
Feedback score
28
Posts
459
Reactions
198
Resources
0
I have no issues with the args. Its the base command that's throwing the error
https://hastebin.com/jilabekuzo.java

Please listen to me, this is your error:
Code:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.nikk.Fess.commands.Ping.onCommand(Ping.java:35) ~[?:?]
This says that your array doesn't have args[0] in it. This is your problem.
 
Last edited:

FelixDev

Typical Developer and Designer
Premium
Feedback score
9
Posts
1,113
Reactions
331
Resources
0
Please listen to me, this is your error:
Code:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.nikk.Fess.commands.Ping.onCommand(Ping.java:35) ~[?:?]
This says that your array doesn't have args[0] in it. This is your problem.
dori is the maaan
 

PurrfectMistake

¬ Owner of AetherTale.com
Premium
Feedback score
10
Posts
307
Reactions
95
Resources
0
Please listen to me, this is your error:
Code:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.nikk.Fess.commands.Ping.onCommand(Ping.java:35) ~[?:?]
This says that your array doesn't have args[0] in it. This is your problem.
The line literally is this:
Player target = Bukkit.getServer().getPlayer(args[0]);
 

Dori

Full Stack Developer
Supreme
Feedback score
28
Posts
459
Reactions
198
Resources
0
The line literally is this:
Player target = Bukkit.getServer().getPlayer(args[0]);
If args.length == 0 args[0] doesn't exist.
This means that anything that will use args[0] will throw a NullPointerException.
 

David Cooke

Java Developer, Discord: Dave#0046
Supreme
Feedback score
1
Posts
27
Reactions
4
Resources
0
Code:
if (args.length == 0);
               {
This is your problem. There's two problems here, the most obvious one is that you are checking if there are no arguments, change the condition to args.length == 1 to check that the player supplies an argument. The less obvious error is the semi colon at the end of the if condition, this causes the code in the block to run regardless due to some features of java. If you have an if statement that only executes one line of code, it is syntactically valid to not include curly braces. E.g.
Code:
if(condition) doSomething();
is equivalent to
Code:
if(condition) {
    doSomething();
}
A semicolon on its own is a valid code block in java which means that your (corrected) if statement parses to:
Code:
if(args.length == 1){

}
{
//rest of code
}
Because curly braces can be used to separate blocks of code in java, your code is syntactically valid although its not doing what you expect.
TLDR:
Change
Code:
if (args.length == 0);
               {
to
Code:
if (args.length == 1) {
 

PurrfectMistake

¬ Owner of AetherTale.com
Premium
Feedback score
10
Posts
307
Reactions
95
Resources
0
Code:
if (args.length == 0);
               {
This is your problem. There's two problems here, the most obvious one is that you are checking if there are no arguments, change the condition to args.length == 1 to check that the player supplies an argument. The less obvious error is the semi colon at the end of the if condition, this causes the code in the block to run regardless due to some features of java. If you have an if statement that only executes one line of code, it is syntactically valid to not include curly braces. E.g.
Code:
if(condition) doSomething();
is equivalent to
Code:
if(condition) {
    doSomething();
}
A semicolon on its own is a valid code block in java which means that your (corrected) if statement parses to:
Code:
if(args.length == 1){

}
{
//rest of code
}
Because curly braces can be used to separate blocks of code in java, your code is syntactically valid although its not doing what you expect.
TLDR:
Change
Code:
if (args.length == 0);
               {
to
Code:
if (args.length == 1) {
yeah fuck me, i cant believe i didn't pick that up.. thanks x'D
Too tired, heading off to bed
 
Status
This thread has been locked.
Top