Player Never Played on Server Error

Discussion in 'Plugin Development' started by gabessdsp, Sep 14, 2013.

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

    gabessdsp

    So I have a command where the args{0} gets turned into a player name. I was told to do this by:

    Player player2 = Bukkit.getServer().getPlayer(args[0]);

    and it works, as long as the player has played on my server before.

    So I am getting this error:
    http://pastie.org/8325616
    which is pointing to this line:

    } else if (player2.isOnline()) {


    so how do I fix this? Let me know if you need any more code.

    Assist
     
  2. Offline

    Squid_Boss

    Try this:
    Code:java
    1. if (p.isOnline() == false) {
    2. return true;
    3. }
     
    gabessdsp likes this.
  3. Offline

    gabessdsp

    So it seemed to fix the error for that line, and now I'm getting an error for this line:

    if (!player2.hasPlayedBefore()) {
    player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", has never played on this server. \n If you believe this is an error then please check your spelling.");
    }

    it's the same error I posted above, and I added the return true myself just now to see if it would fix it but it does not.
     
  4. Offline

    Pizza371

    gabessdsp that line needs to be below this;
    Code:
    if (p.isOnline() == false) {
    return true;
    }
    if it isn't, because you'd be trying to check if null has played before
     
    gabessdsp likes this.
  5. Offline

    Quantix

    The method Bukkit.getPlayer() always returns either an online Player (if it can match that string with one of the players on the server) or null if it can't find a matching player. If it can't find a matching player and returns null and you use any of the methods inside the null Player object such as isOnline() it will create the NPE in your stack trace. Use:
    Code:java
    1. Player player = Bukkit.getPlayer("nameOfPlayer");
    2.  
    3. if (player != null) {
    4. //your code here
    5. }
    6.  
    7.  
     
    gabessdsp likes this.
  6. Offline

    gabessdsp

    Pizza371
    Assist
    Squid_Boss

    I did what Pizza said but it still says there's an erro on line 72 causing a nullpointerexception. Line 72 is the first line in the paste.

    http://pastie.org/8334482

    Quantix

    I didn't see your post before my last post, I'll have to try that.

    Here's my whole bit of code. I already have what Quantix said, but i still get an error NullPointerException ot line 72 which is : if(player2.isOnline() == false)


    http://pastie.org/8334493

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

    Janmm14

    Use Bukkit.getOfflinePlayer(playerName); to get an offline playee then check if he was online before if you want to know that.
     
  8. Please Paste the whole code. line 72 != paste

    I think Janmm14 has solved the problem!
     
  9. Offline

    gabessdsp


    Janmm14

    It seems that the checking if he is online before is what is causing the error message. If the player is not online it gives an error and stops there without anymore checks.


    ysl3000

    I did post the whole code for line 72. Read the previous messages.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  10. And Janmm14 wrote that you should use the offlinePlayer.
     
  11. Offline

    gabessdsp


    ysl3000

    And he also said though if I need to check for online player I need to check that first. The part of checking for onlineplayer and if the player is offline is causing the problem. The sequence stops at the checking if the player is online and causes the error.
     
  12. Bukkit.getOfflinePlayer("playername").isOnline() :D
     
  13. Offline

    gabessdsp

    ysl3000

    So I just replaced some of my code with that and we went into a giant circle where I now get an error on if the player has played before.

    http://pastie.org/8334622

    So here is all the code that should be needed to diagnose this issue.

    I am getting a NullPointerException when I check if the player has played before. So how can I have this code do the same thing but get around the nullpointer exception, I did try a try-catch statement but then it wouldn't ever move on to my last if statement.


    http://pastie.org/8335698

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  14. Use the offline player check the methods beyond.

    http://jd.bukkit.org/dev/apidocs/





    getFirstPlayed

    long getFirstPlayed()
    Gets the first date and time that this player was witnessed on this server.
    If the player has never played before, this will return 0. Otherwise, it will be the amount of milliseconds since midnight, January 1, 1970 UTC.

    Returns:
    Date of first log-in for this player, or 0
     
  15. Offline

    blablubbabc

    I don't see, where you are checking for null, like quantix suggested it... getPlayer(..) will return null, if the player with this name is not online. You can't run methods on an object which is null.
     
Thread Status:
Not open for further replies.

Share This Page