1.8.8 NullPointer in Friend Plugin

Discussion in 'Spigot Plugin Development' started by Deagan, Jan 12, 2020.

  1. Hello, I am new to plugin coding and i keep getting a null pointer exception on line 29.

    Here is the code:

    Code (Java):

    public class Commands implements CommandExecutor, Listener {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("friend")) {
            if(args.length == 1) {
            String target = args[0];
            Player ptarget = Bukkit.getPlayer(target);
            if (Bukkit.getServer().getPlayer(target) != null && Bukkit.getServer().getPlayer(target).getName().equalsIgnoreCase(target)){
                if(Main.plugin.getConfig().getString(ptarget.getUniqueId().toString() + ".FRequests").contains(p.getName())) {
                else {
                    p.sendMessage("no, but actually yes.");
                    Main.plugin.getConfig().set(ptarget.getUniqueId().toString() + ".FRequests", p.getName());
            } else { p.sendMessage(Variables.Strings.prefix + target.toString() + " is not online!"); }
            } else { p.sendMessage(Variables.Strings.prefix + "Invalid subcommand!"); }
            return true;
  2. cool. so wheres line 29 and can we see the config?
  3. this is like 29:
    if(Main.plugin.getConfig().getString(ptarget.getUniqueId().toString() + ".FRequests").contains(p.getName())) {

    and here is the config

    Name: CeleryLOL
    FRequests: xDeagan
  4. in that line, either Main.plugin (abuse of static) is null, or the string gotten from the config is null. to figure out which it is, print both values with System#out or somewhere you can see the message.
  5. Strahan


    Aside from ensuring your plugin instance is valid, don't stack methods on other methods that get their results from sources that may be invalid. I always treat externally obtained data as if it was broken. Either pass a default value or if that isn't applicable, read the String into a variable then validate it before trying to perform operations on it.