Solved NullPointerException. help?

Discussion in 'Plugin Development' started by jamiemac262, Mar 13, 2013.

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

    jamiemac262

    so... I have been trying everything I can think of to fix this, I just don't understand what's wrong :/

    the function SHOULD get and display the target player's gamemode.

    I know that: the SendAIPrivateMessage() works perfectly i'm using it in most of the plugin

    here's where the stack trace:

    Code:
     [SEVERE] Could not pass event AsyncPlayerChatEvent to ServerAI v3.4.7
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
        at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.java:903)
        at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:853)
        at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_R1.NetworkManager.i(NetworkManager.java:208)
        at net.minecraft.server.v1_4_R1.NetworkManager.c(NetworkManager.java:344)
        at net.minecraft.server.v1_4_R1.NetworkReaderThread.run(SourceFile:93)
    Caused by: java.lang.NullPointerException
        at me.jamiemac262.ServerAIReWrite.ServerChatListener.onPlayerChat(ServerChatListener.java:220)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 9 more
    I also know the plugin reaches the first noPerms() method as it displayed that message

    the noPerms() method is in there so I could track what line the program crashes at

    it looks like target = target.getPlayer() is causing the crash

    here is the function:

    Code:
    if (containsString(Playermessage, "sai") && containsString(Playermessage, "check") && containsString(Playermessage, "gamemode")) {
                    if (p.hasPermission("sai.check")) {
                        chat.setCancelled(true);
     
                        new SendPrivateAIMessage(p, 0.5, "Warning: My AI has not finished learning this function","Warning: My AI has not finished learning this function","Warning: My AI has not finished learning this function");
                        Player target = findPlayerInArray(Playermessage);
                        noPerms();
                        Player target = target.getPlayer();
                        noPerms();
                        new SendPrivateAIMessage(p, 0.5, "checking gamemode for" + target.getDisplayName(), "ok this will take a second", "ok let me check my memory circuits for " + target.getDisplayName());
                        noPerms();
                        Gamemode mode = new Gamemode(target);
                        noPerms();
                        String gamemode = mode.check();
                        noPerms();
                        new SendPrivateAIMessage(p, 0.5, "it seems that " + target.getDisplayName() + "is in" + gamemode, "it seems that " + target.getDisplayName() + "is in" + gamemode, "it seems that " + target.getDisplayName() + "is in" + gamemode);
                    } else {
                        noPerms();
                    }
                }
    if anyone needs anything else let me know please, i'm getting nowhere with this :( any help much appreciated :)
     
  2. Offline

    fireblast709

    findPlayerInArray(Playermessage) returns null.
     
  3. Offline

    jamiemac262

    thanx fireblast, can you take a look at the method for me? I cant work out why it returns null :/

    Code:
    public static Player findPlayerInArray(String[] playernames) {
            Player foundPlayer = null;
            Player[] player = Bukkit.getOnlinePlayers();
           
            for (int i = 0; i < playernames.length; i++) {
               
                for (int j = 0; j < player.length; j++) {
                    String play = player[j].toString();
                    if(play.equals(playernames[i])){
                    foundPlayer = player[j];
               }
                   
                }         
            }
           
            return foundPlayer;
        }
     
  4. Offline

    kreashenz

    The Player foundPlayer = null; Its checking if the player is NULL.. Should it be like that or not ?
     
  5. Offline

    jamiemac262

    it should, it's initiating the foundPlayer variable

    -.- I just solved the problem
    String play = player[j].toString();
    was giving craftplayer=<player name>

    so for anybody with similar problems

    instead of
    String play = player[j].toString();
    use

    String play = player[j].getDisplayName();

    thanx for everyone who tired to help :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page