NullPointer Issues with PlayerOnline

Discussion in 'Plugin Development' started by gabessdsp, Dec 6, 2013.

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

    gabessdsp

    What my code is doing:
    Okay so here is the issue, I type in /last seen playername while they are online and it tells me that the player is online which is what it should do. But if the player is offline it no longer throws an exception in the console...but it also doesn't tell me anything in the chat like I expected it to do.


    What I need my code to do:
    I need my code to tell the me that a player is online if the command is used while the player is online. If the player is offline I need the code to tell me when they last joined (already have my code for lastjoined). If the player has never played on the server before I need to display an error message stating that Player X has never played before or you mis-spelled their name"

    I have tried so many different ways but I think I'm just not setting up the logic correctly.


    My Code:
    Code:java
    1. Player player2 = Bukkit.getPlayer(args[0]);
    2.  
    3.  
    4. try {
    5. if (player2 != null) {
    6.  
    7. if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    8. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", is " + ChatColor.GREEN + "Online" + ChatColor.GOLD + ".");
    9. } else {
    10.  
    11. long timeNow = System.currentTimeMillis();
    12. long timeLast = timeNow - getConfig().getLong(args[0]);
    13. long days = timeLast / 0x5265c00L;
    14. timeLast %= 0x5265c00L;
    15. long hours = timeLast / 0x36ee80L;
    16. timeLast %= 0x36ee80L;
    17. long minutes = timeLast / 60000L;
    18. timeLast %= 60000L;
    19. long seconds = timeLast / 1000L;
    20. if (days > 0L) {
    21. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + days + " day(s), and " + hours + " hour(s) ago.");
    22. } else if (days == 0L && hours > 0L) {
    23. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + hours + " hour(s), and " + minutes + " minute(s) ago.");
    24. } else {
    25. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + minutes + " minute(s), and " + seconds + " second(s) ago.");
    26. }
    27. }
    28. }
    29. } catch (NullPointerException ex) {
    30. 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.");
    31. }
     
  2. Offline

    PogoStick29

    First off, check if args has enough data (looks like you need one String).
    Second, "/last seen playername" would return data for the player "seen". I assume you mean "/lastseen playername"
     
  3. Offline

    gabessdsp


    yes I mean /lastseen

    I already have the args check in there it's just not shown. This is inside after the args check because it's the only necessary bits that are needed to fix my problem.
     
  4. Offline

    PogoStick29

    Don't catch a NullPointerException, just make sure that any variable you are going to use is not null. Also, to neaten up your code, you should check if the variable is null and if it is, return inside the if statement. That way, you don't need to put your code inside of a bunch of if statements.
     
  5. Offline

    gabessdsp



    That's the thing, the variable can be null. If they type in "gabessdsxp" instead of "gabessdsp" assuming a gabessdsxp has never played on their server it would then throw the exception and the command will fail.
     
  6. Offline

    PogoStick29

    What I meant was, instead of surrounding your code in a try/catch, do:

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

    if (target == null) {
    // send message;
    return true;
    }

    // do some code here.

    This method is detailed in one of my first videos, so give it a look.
     
  7. Offline

    gabessdsp


    I think that's the first way I tried doing this, and it couldn't accomplish the task at hand. Let me just try re-writing it again this way and see what happens.
     
  8. Offline

    AoH_Ruthless

    gabessdsp
    What he is saying is you don't need a try/catch block outside your if statements.
    You already are using "if(player2 != null) {}", after the bracket that closes that if statement, make an else statement and send a message to the original player.

    EDIT: Damn it PogoStick29
     
    PogoStick29 likes this.
  9. Offline

    gabessdsp

    gabessdsp AoH_Ruthless

    So here's the new code:
    Code:java
    1. Player player2 = Bukkit.getPlayer(args[0]);
    2.  
    3. if (player2 != null) {
    4. if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    5. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", is " + ChatColor.GREEN + "Online" + ChatColor.GOLD + ".");
    6. } else {
    7.  
    8. long timeNow = System.currentTimeMillis();
    9. long timeLast = timeNow - getConfig().getLong(args[0] + ".LastLogoutTime");
    10. long days = timeLast / 0x5265c00L;
    11. timeLast %= 0x5265c00L;
    12. long hours = timeLast / 0x36ee80L;
    13. timeLast %= 0x36ee80L;
    14. long minutes = timeLast / 60000L;
    15. timeLast %= 60000L;
    16. long seconds = timeLast / 1000L;
    17. if (days > 0L) {
    18. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + days + " day(s), and " + hours + " hour(s) ago.");
    19. } else if (days == 0L && hours > 0L) {
    20. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + hours + " hour(s), and " + minutes + " minute(s) ago.");
    21. } else {
    22. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + minutes + " minute(s), and " + seconds + " second(s) ago.");
    23. }
    24. }
    25. } else {
    26. 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.");
    27. }



    Here is what happens. If the user is online it tells me the user is online, if I type in a wrong name it tells me that player has never played or I mis-spelled the name.

    But if the player has played before, but is offline It never gets to the point of showing when they were last played. So if the player is offline then they are automatically null, so how do I display the part where it shows that the player was last seen x time ago?

    Also PogoStick29 idk why I tagged myself. Meant to tahg you

    Also just tried this with the same result:

    Code:java
    1. Player player2 = Bukkit.getPlayer(args[0]);
    2.  
    3. if (player2 != null) {
    4. if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    5. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", is " + ChatColor.GREEN + "Online" + ChatColor.GOLD + ".");
    6. }
    7. } else {
    8.  
    9. try {
    10. if (player2.hasPlayedBefore()) {
    11.  
    12. long timeNow = System.currentTimeMillis();
    13. long timeLast = timeNow - getConfig().getLong(args[0] + ".LastLogoutTime");
    14. long days = timeLast / 0x5265c00L;
    15. timeLast %= 0x5265c00L;
    16. long hours = timeLast / 0x36ee80L;
    17. timeLast %= 0x36ee80L;
    18. long minutes = timeLast / 60000L;
    19. timeLast %= 60000L;
    20. long seconds = timeLast / 1000L;
    21. if (days > 0L) {
    22. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + days + " day(s), and " + hours + " hour(s) ago.");
    23. } else if (days == 0L && hours > 0L) {
    24. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + hours + " hour(s), and " + minutes + " minute(s) ago.");
    25. } else {
    26. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + minutes + " minute(s), and " + seconds + " second(s) ago.");
    27. }
    28.  
    29. } else {
    30. 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.");
    31. }
    32.  
    33. } catch (NullPointerException ex) {
    34. 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.");
    35.  
    36. }
    37. }


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

    AoH_Ruthless

    gabessdsp
    You need to learn how to read the if/else statements.
    But, I don't understand. Why are you checking if a player is online, twice? Remove the Bukkit.getofflinePlayer ....

    Code:java
    1. if(player2 != null) {
    2. if(Bukkit.getOfflinePlayer(args[0]).isOnline()){ //You are doing it twice.
     
  11. Offline

    gabessdsp

    I did that on my own after this post, but it still doesn't fix the problem. Like I said though it might be something like I have the logic wrong. I just can't wrap my mind around this one particular thing and that is why I am asking for help.
     
  12. Offline

    AoH_Ruthless

    gabessdsp
    Ok, please repaste your code. It seems as if you are trying different things at random in the hopes that your plugin works so I need something updated to work with. Just mention if you are using code from an above post.
     
  13. Offline

    gabessdsp

    AoH_Ruthless

    This is my current code:
    Code:java
    1. Player player2 = Bukkit.getPlayer(args[0]);
    2.  
    3. if (player2 != null) {
    4. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", is " + ChatColor.GREEN + "Online" + ChatColor.GOLD + ".");
    5. } else {
    6.  
    7. try {
    8.  
    9. long timeNow = System.currentTimeMillis();
    10. long timeLast = timeNow - getConfig().getLong(args[0] + ".LastLogoutTime");
    11. long days = timeLast / 0x5265c00L;
    12. timeLast %= 0x5265c00L;
    13. long hours = timeLast / 0x36ee80L;
    14. timeLast %= 0x36ee80L;
    15. long minutes = timeLast / 60000L;
    16. timeLast %= 60000L;
    17. long seconds = timeLast / 1000L;
    18. if (days > 0L) {
    19. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + days + " day(s), and " + hours + " hour(s) ago.");
    20. } else if (days == 0L && hours > 0L) {
    21. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + hours + " hour(s), and " + minutes + " minute(s) ago.");
    22. } else {
    23. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + minutes + " minute(s), and " + seconds + " second(s) ago.");
    24. }
    25.  
    26.  
    27. } catch (NullPointerException ex) {
    28. 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.");
    29.  
    30. }
    31. }


    This code successfully tells if the player is online, but once offline it now tells me when they were last seen, but if I type in a name of a player who's never played before it tries to tell me when they were last seen but instead I need it to go to the message in the catch statement.
     
  14. Offline

    AoH_Ruthless

    gabessdsp
    Get rid of the try/catch block, as I said before.
    Instead, check if the player has /has not played before. If they have, do that seen message. If they haven't, send the message that says the player has never been online.
     
  15. Offline

    gabessdsp

    AoH_Ruthless

    I believe a few posts up that's what I have and it resulted in the same problem. But here is what I have now:


    Code:java
    1. Player player2 = Bukkit.getPlayer(args[0]);
    2.  
    3. if (player2 != null) {
    4. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", is " + ChatColor.GREEN + "Online" + ChatColor.GOLD + ".");
    5. } else {
    6.  
    7. if (player2.hasPlayedBefore()) {
    8.  
    9. long timeNow = System.currentTimeMillis();
    10. long timeLast = timeNow - getConfig().getLong(args[0] + ".LastLogoutTime");
    11. long days = timeLast / 0x5265c00L;
    12. timeLast %= 0x5265c00L;
    13. long hours = timeLast / 0x36ee80L;
    14. timeLast %= 0x36ee80L;
    15. long minutes = timeLast / 60000L;
    16. timeLast %= 60000L;
    17. long seconds = timeLast / 1000L;
    18. if (days > 0L) {
    19. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + days + " day(s), and " + hours + " hour(s) ago.");
    20. } else if (days == 0L && hours > 0L) {
    21. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + hours + " hour(s), and " + minutes + " minute(s) ago.");
    22. } else {
    23. player.sendMessage(ChatColor.GOLD + "Player, " + ChatColor.AQUA + args[0] + ChatColor.GOLD + ", was last seen " + minutes + " minute(s), and " + seconds + " second(s) ago.");
    24. }
    25. } else {
    26. 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.");
    27.  
    28. }
    29. }



    Now, correct me if I am wrong but that is what you told me to do?

    But with doing it this way I get the NullPointer Error even if the player has played before and the name is correct:

    http://pastie.org/8534905
     
  16. Offline

    AoH_Ruthless

    gabessdsp
    Which line is line 80?

    EDIT: Yes, that is what I said to do.

    Maybe add return statements to each bracket block to avoid fall-through of code.
     
  17. Offline

    gabessdsp

    AoH_Ruthless

    This is line 80:

    if (player2.hasPlayedBefore()) {

    and what should I return though? Just add return: true; to each statement or what?
     
  18. Offline

    AoH_Ruthless

    gabessdsp
    It depends if the whole method is boolean or void, and what you want. I would assume return true;, though.

    And I think you are receiving an NPE because using the .hasPlayedBefore() refers to offline players. I think, at least.
     
  19. Offline

    gabessdsp

    AoH_Ruthless

    So I added the return true statements where they fit.

    Now yes, I am getting the NPE because the .hasPlayedBefore() refers to offline players. That's why about a month ago when I was trying to solve this problem I was told to use a try-catch, which doesn't fix it. That's why I am still stuck on this problem and it's been about....5 months total I've had several discussions up just trying to fix the issue. I can do one or the other, but it seems I cannot do both. I need to know how to make it work with both though.

    For now I just used a workaround, where it will just display their last logon, but if the player doesn't exist it displays their last logon was 1605 days + x hours ago and so on. So I made it check if it's great than 1600 then it displays the user has never joined message. Like I said it's just a temporary fix. It'll only work for the next...4 and a half years so I have to get it fixed by then lol :3

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

Share This Page