Solved Why does this return me NullPointerException at my plugin?!

Discussion in 'Plugin Development' started by stimoze, Oct 27, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys, I was developing a plugin, and I found a little problem. Here's my code.
    Code:
    package soleplugin;
    
    import java.util.ArrayList;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ChatCall implements Listener {
        public static Org plg;
        public ChatCall(Org org) {
            plg = org;
        }
       
        ArrayList<Player> chatting;
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void CallingSole(AsyncPlayerChatEvent e){
            String[] solec = e.getMessage().split(" ");
            if (e.getMessage().contains("hey sole")){
                chatting.add(e.getPlayer());
                e.setCancelled(true);
                e.getPlayer().sendMessage("§bHey " + e.getPlayer().getName() + "! I'm ready for assistance. Remember if you need help just write.");
            }
            if (chatting.contains(e.getPlayer())){
                if (e.getMessage().contains("help")){
                    e.setCancelled(true);
                    e.getPlayer().sendMessage("Here you are, " + e.getPlayer().getName() + "!");
                    e.getPlayer().sendMessage("Here you can find all the commands and words you can use.");
                    e.getPlayer().sendMessage("§cLegend: (optional words) [logic sections] <placeholders>");
                    e.getPlayer().sendMessage("§3Give [me/<player name>] <amount> (of) <item-name or ID> §f- §2Give an item to you or a player.");
                }
                if (e.getMessage().contains("bye sole")){
                    e.getPlayer().sendMessage("Bye! See you next time! :)");
                }
                //Commandsection
                if (e.getMessage().contains("give")){
                    e.setCancelled(true);
                    try{
                        if (solec[1].equals("me")){
                            e.getPlayer().getInventory().addItem(new ItemStack(Integer.getInteger(solec[2])));
                        }
                    }
                    catch(Exception exc) {
                        e.getPlayer().sendMessage("I couldn't understand your text properly. Please just write for help.");
                    }
                }
            }
        }
    
    }
    
    Can you guys tell me why does this return a nullpointerexception?

    My plugin is a chat assistant plugin.

    Code:
    [16:16:31 ERROR]: Could not pass event AsyncPlayerChatEvent to SoleVirtChat v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_111]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_111]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
            at soleplugin.ChatCall.CallingSole(ChatCall.java:24) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
            ... 11 more
     
  2. Offline

    timtower Administrator Administrator Moderator

    @stimoze You never initialize chatting.
    And please make chatting a private variable, make it use UUID or names
    Make plg a private non-static variable.
     
  3. Thank you I solved it! :)
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page