NullPointerExeption when I get an OfflinePlayer display name

Discussion in 'Plugin Development' started by HeavyMine13, Jul 18, 2014.

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

    HeavyMine13

    Hello! When I try to get an Offline Player display name or first join or latest join it return null pointer exception!!!

    Code:
    Code:java
    1. Player infoed = (Player) Bukkit.getServer().getPlayer(
    2. args[0]);
    3. OfflinePlayer p = (OfflinePlayer) infoed;
    4. sender.sendMessage(ChatColor.GOLD + "IGN: "
    5. + infoed.getDisplayName());
    6. sender.sendMessage(ChatColor.GOLD + "First Joined: "
    7. + ChatColor.GREEN
    8. + new Logorizer(p.getFirstPlayed()).toString());
    9. sender.sendMessage(ChatColor.GOLD + "Last Joined: "
    10. + ChatColor.GREEN
    11. + new Logorizer(p.getLastPlayed()).toString());
     
  2. Offline

    FabeGabeMC

    Could you show us the full stack trace?
     
  3. Offline

    xTigerRebornx

    HeavyMine13 Use Bukkit#getOfflinePlayer(), Bukkit#getPlayer() will return null if the Player isn't online.
     
  4. Offline

    FabeGabeMC

    xTigerRebornx
    In that case he would have just done:
    Code:java
    1. OfflinePlayer infoed = Bukkit.getServer().getOfflinePlayer(args[0]);

    am I right?
     
  5. Offline

    fireblast709

    FabeGabeMC yes
    HeavyMine13 be wary of the fact that it could block the main thread (due to the fact that it will use HTTP to fetch the UUID)
     
  6. Offline

    _LB

    That would be nice, but no - a player has to have connected to the server previously for Bukkit to have information on them. If they haven't connected the returned player name is null.
     
  7. Offline

    fireblast709

    _LB it will attempt to get an uuid from the mojang servers, and since you already use the name with getOfflinePlayer, you will never get a null value.
     
  8. Offline

    _LB

    That's not my experience at all, what CB version are you testing with?
     
  9. Offline

    fireblast709

    _LB I refer to the latest source as found on github
     
  10. Offline

    _LB

    I figure out where we were confused - I fetch OfflinePlayers by UUID in my code, I didn't realize fetching by name would grab their UUID. Sorry for the confusion.
     
  11. Offline

    HeavyMine13

    hmm, im using Player then Offline player cz i need its displayname which is a player method

    It needs to be a Player for some methods...

    Sorry for the weird alignment of this text, console of mcprohosting

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Offline

    FabeGabeMC

    HeavyMine13
    What do you have on line 52?
     
  13. Offline

    fireblast709

    HeavyMine13 you cannot use Player when they are offline, since that is null -> NPE
     
  14. Offline

    HeavyMine13

    Solution for this? I NEED Player to get some important info!
     
  15. Offline

    fireblast709

    HeavyMine13 if the player is not online, you cannot get a Player object, simple as that. Just store the data you need in a Map when they quit, or use OfflinePlayer. What kind of data do you need anyway?
     
    FabeGabeMC likes this.
Thread Status:
Not open for further replies.

Share This Page