Solved Getting null exception

Discussion in 'Spigot Plugin Development' started by Jacko2k18, Aug 2, 2020 at 1:43 AM.

  1. Hello, I fixed a few errors earlier with the help of this forum, I am getting a java null exception error when trying to use the command "/wl add 8YQ" on my plugin.

    Here's the commands class :
    Code (Java):
    package me.Jacko.Whitelist.Commands;

    import java.util.LinkedList;

    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;

    import me.Jacko.Whitelist.Whitelist;

    public class WhitelistAddCMD implements BaseCMD {

        Whitelist main;
         
        void Whitelistt(Whitelist main) {
            this.main = main;
        }
       
        @Override
        public boolean execute(Player player, Command cmdObj, String label, String cmd, LinkedList<String> args) {
                   
            Whitelist.whitelisted.add(cmd.toString().trim());
           
            main.getConfig().set("Whitelisted", Whitelist.whitelisted);
           
            main.saveConfig();
           
            return true;
           
        }
       
    }
    Here's the Command Handler class :
    Code (Java):
    package me.Jacko.Whitelist.Commands;

    import java.util.Arrays;
    import java.util.LinkedList;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    import me.Jacko.Whitelist.Utils.Utils;


    public class WhitelistPlayer implements CommandExecutor, Listener {
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmdObj, String label, String[] args) {
            label = label.toLowerCase();
           
            if(sender instanceof Player) {
                if(args.length == 0) {
                    sender.sendMessage(Utils.chat("&e --- Jacko's Whitelist Help --- "));
                    sender.sendMessage(Utils.chat("&a/" + label + " add - Whitelists a player."));
                    sender.sendMessage(Utils.chat("&a/" + label + " remove - Unwhitelist a player."));
                    sender.sendMessage(Utils.chat("&a/" + label + " toggle - Toggle the whitelist on/off."));
                    sender.sendMessage(Utils.chat("&e ------------------------------ "));
                }
                else {
                   
                    String cmd = args[0];
                    if(cmd.equalsIgnoreCase("add")) {
                        if(args.length == 1) {
                            sender.sendMessage(Utils.chat("&a/" + label + " add " + sender.getName() + " - Whitelists a player."));
                        }
                        else {
                           
                            LinkedList<String> list = new LinkedList<String>(Arrays.asList(args));
                            list.pollFirst();
                            list.pollFirst();
                            return new WhitelistAddCMD().execute((Player) sender, cmdObj, label, args[1], list);
                           
                           
                           
                        }
                    }
                   
                   
                }
            } else {
                sender.sendMessage(Utils.chat("&cYou must be a player to do that!"));
            }
           
            return true;
        }

       
       
    }
     
    and here is the player join listener class :
    Code (Java):
    package me.Jacko.Whitelist.Listeners;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import me.Jacko.Whitelist.Whitelist;
    import me.Jacko.Whitelist.Utils.Utils;

    public class ListenerJoin implements Listener {
       
        Whitelist main;
         
        void Whitelistt(Whitelist main) {
            this.main = main;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
           
            if(Whitelist.whitelisted.contains("8YQ")) {
               
                System.out.println("Player " + e.getPlayer().getName() + "is whitelisted.");
               
            }
            else {
               
                e.getPlayer().kickPlayer(Utils.chat("&4&lALERT &8: &cYou are not whitelisted noob."));
            }
           
        }
       
    }
     
  2. tell us which line the error is throwing at
     
  3. Errors are here :
    WhitelistAddCMD.java:23
    WhitelistPlayer.java:40
     
  4. 1. Why are you using linkedlist instead of a regular String array?

    2.
    that's not how you create a constructor for the WhitelistAddCMD class

    3.
    You need to pass the instance of your plugin into the constructor.
     
  5. I need to know how to pass the instance of my plugin, I don't know how to do it, I have tried almost everything I can think of...
     
  6. Sorry, I got that working but when I am trying to add a string to the list it gives me java nullpointer exception.
    Error :
    Code (Java):
    Caused by: java.lang.NullPointerException

    at me.Jacko.Whitelist.Whitelist.addItem(Whitelist.java:53) ~[?:?]

    at me.Jacko.Whitelist.Commands.WhitelistAddCMD.execute(WhitelistAddCMD.java:18) ~[?:?]

    at me.Jacko.Whitelist.Commands.WhitelistPlayer.onCommand(WhitelistPlayer.java:40) ~[?:?]