Getting online players always returns null

Discussion in 'Plugin Development' started by jimbo8, Aug 2, 2014.

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

    jimbo8

    Hey! :)

    I'm working on a project where i need to get the amount of all online players. What i'm doing is calling the getOnlinePlayers()-method and checking the length of it(.length), but it always gives me an NPE. Any ideas? It should not be null. It even returns null when there are players online, and an integer should not return null either.
     
  2. Offline

    Necrodoom

    Erm, paste the code please?
     
  3. Offline

    jimbo8

    Sure.

    Code:java
    1. @EventHandler
    2. public void onPing(ServerListPingEvent event) {
    3. int amp = Bukkit.getServer().getOnlinePlayers().length;
    4. if (amp < pl.getConfig().getInt("max-players-before-deny")) {
    5. event.setMotd(ChatColor.RED
    6. + "The server is full.");
    7. }
    8. }
     
  4. Offline

    ResultStatic

    jimbo8 i dont think amp is returning null, i think plugin is returning null 'pl' show us ur instanceof plugin
     
  5. Offline

    jimbo8

    Haha, I'm sorry.

    When i've been messing around with my plugin, i didn't actually save it in the right server-folder. I did some more debugging and the config is the one returning null, even though it does have a value. The path is as you can see in the code and the value is "5" for testing. Also, i know that the math is wrong, but this is for testing only.

    Still says that it is null, though.
     
  6. Offline

    TGF

    Maybe try this:
    Code:java
    1.  
    2. public static int getPlayersOnline(){
    3. int x = 0;
    4. try {
    5. x = Bukkit.getServer().getOnlinePlayers().length;
    6. } catch (NullPointerException e) {
    7. x = 0;
    8. }
    9. return x;
    10. }
     
  7. Offline

    Gater12

    jimbo8
    Did you instantiate 'pl'?
     
  8. Offline

    jimbo8

    Yeah.

    The plugin compiles, but i get a NPE when refreshing my serverlist. Gater12
     
  9. Offline

    AoH_Ruthless

    TGF
    Why would you catch a NullPointerException. It's impossible for one to be triggered because x is an primitive integer and can't be null.

    jimbo8
    That is not what Gater12 means by instantiating your plugin. Show us your global field and constructor, please.
     
Thread Status:
Not open for further replies.

Share This Page