NullPointerException on a for-each loop :S?

Discussion in 'Plugin Development' started by vanZeben, Jul 19, 2011.

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

    vanZeben

    Ive been going over this error for about 10 minutes now and can seem to figure out why. Typically they are easy to figure out and correct but I'm getting a null pointer exception on the line for(Player player : getServer().getOnlinePlayers()) {
    Any help is appreciated :D

    Method (open)

    Code:
    public String checkOnlineTown (Town town,String r) {
            try {
                for(Player player : getServer().getOnlinePlayers()) {
                    System.out.println(player.getName());
                    try {
                        Resident  resident = getTownyUniverse().getResident(player.getName());
                        if (resident.hasTown() && resident.getTown().equals(town)) {
                            r += resident.getName() + ", ";
                        }
                    } catch (NotRegisteredException e) {
                        e.printStackTrace();
                    }
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            return r;
        }


    Error (open)

    Code:
    11:02:20 [SEVERE] java.lang.NullPointerException
    11:02:20 [SEVERE] 	at ca.xshade.bukkit.towny.Towny.checkOnlineTown(Towny.java:382)
    11:02:20 [SEVERE] 	at ca.xshade.bukkit.towny.TownyFormatter.getStatus(TownyFormatter.java:138)
    11:02:20 [SEVERE] 	at ca.xshade.bukkit.towny.object.TownyUniverse.getStatus(TownyUniverse.java:575)
    11:02:20 [SEVERE] 	at ca.xshade.bukkit.towny.command.TownCommand.parseTownCommand(TownCommand.java:106)
    11:02:20 [SEVERE] 	at ca.xshade.bukkit.towny.command.TownCommand.onCommand(TownCommand.java:77)
    11:02:20 [SEVERE] 	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35)
    11:02:20 [SEVERE] 	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:129)
    11:02:20 [SEVERE] 	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:320)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:713)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:677)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:670)
    11:02:20 [SEVERE] 	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
    11:02:20 [SEVERE] 	at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
    11:02:20 [SEVERE] 	at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
    11:02:20 [SEVERE] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
    11:02:20 [SEVERE] 	at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    
     
  2. Offline

    xpansive

    All I can think of is there's no players online, or resident is null...
     
  3. Offline

    nossr50

    Code:
     for(Player player : getServer().getOnlinePlayers()) {
        if(player != null)
            {
            //DO STUFF
            }
    }
    You don't need those Try/Catch's
     
  4. Offline

    vanZeben


    yea i realize that, however the error log i was getting was very messy so i figured it could help me decipher and they had been taken out since this paste. Also this is still giving me the NullPointer. And its impossible to have no players online when this is called because I'm invoking it VIA an in game command. The for-each loop seems to work fine in the onEnable when the code is copied/pasted in. However in its own method it gives error. Is there anything i have to do to instantiate the plugin before i call getServer()?
     
  5. Offline

    nossr50

    Are you sure the objects you are passing to the method are not null?
     
  6. Offline

    vanZeben

    Positive, I am passing a null-string however but that shouldn't matter because Java isn't even getting to read the lines that those variables are located on. It stops at the getServer().getOnlinePlayers();

    Ive edited the code for a true for loop and its still giving the error on the getServer()

    Code:
    public String checkOnlineStatus (Town town, String r) {
            Player[] players = getServer().getOnlinePlayers();
    
            for (int i = 0;i<players.length;i++) {
                Resident resident;
                try {
                    resident = getTownyUniverse().getResident(players[i].getName());
                    if (resident.hasTown() && resident.getTown().equals(town)) {
                        r += resident.getName() + ", ";
                    }
                } catch (NotRegisteredException e) {
                    e.printStackTrace();
                }
            }
            return r;
        }
     
  7. Offline

    nisovin

    Where are you calling this code from? You aren't calling it from the constructor are you? Have you tried Bukkit.getServer()?
     
  8. Offline

    vanZeben

    no its being called from a onCommand and I have tried Towny plugin = new Towny(); plugin.getServer() with no avail however the Bukkit.getServer() does work either..... I decided to debug a little more and added System.out.println(Bukkit.getServer().getOnlinePlayers()); as well as the other variations(plugin.getServer()/this.getServer()/getServer()), and its printing out null :S? even with more then one person on the server :S

    Nevermind got it working. Used methods already implemented (getPlugin().getServer().getOnlinePlayers())

    Thanks for trying to help people :D
     
Thread Status:
Not open for further replies.

Share This Page