for(Player player)?

Discussion in 'Plugin Development' started by SparkWings, Jun 12, 2014.

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

    SparkWings

    Okay, I'm getting an obvious NullPointer on this code right here
    Code:java
    1.  
    2. @SuppressWarnings("null")
    3. public static Player getExactPlayer(String name)
    4. {
    5. for (Player player = null;;)
    6. if (player.getName().equals(name)) {
    7. return player;
    8. }
    9. }
    10.  
    11.  


    Can someone tell me how to fix this NullPointer by just defining the player?
     
  2. Offline

    drtshock

  3. Offline

    Unknowncmbk

    Please watch thenewboston Tutorials, that for loop does absolutely nothing.


    Code:java
    1. for(Player p : Bukkit.getOnlinePlayers())
    2. {
    3. if (player.getName().equals(name))
    4. return player;
    5. }



    On line 5 of your code you're never changing the reference to the Player, as you declare it's initialization to be null, and you never update that reference.
     
  4. Offline

    Necrodoom

    Unknowncmbk Bukkit.getOnlinePlayers[]?

    drtshock
    He's doing the conventional for loop that doesn't really do anything instead of a for-each.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 15, 2019
  5. Offline

    Garris0n

    Yeah, except you're supposed to provide it with something to iterate over...
     
    xTrollxDudex likes this.
  6. Offline

    SparkWings

    drtshock Okay, I fixed that, but what about this. I have
    Code:java
    1. @SuppressWarnings("null")
    2. public static boolean isPlayerInTrade(String name)
    3. {
    4. Validate.notNull(name, "Name cannot be null");
    5. for(Trade trade = null;;)
    6. if (trade.containsPlayer(name)) {
    7. return true;
    8. }
    9. }


    and I'm getting another NPE.
     
  7. Offline

    HeadGam3z

  8. Offline

    1Rogue

    Because "trade" is null. You're dereferencing a null object. NullPointerExceptions happen when you dereference a null object.

    What are you expecting?

    Where are you magically getting "trade" from? Is there only ever 1 single trade in the existence of ever?

    Using a for loop in the method you're describing, while syntactically legal will cause an infinite loop (provided you not NPE'ing first) when it doesn't find a result.

    Read the java documentation about for-each: http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
     
    AoH_Ruthless likes this.
  9. Offline

    SparkWings

    1Rogue I have another class called Trades, and in that class I have a

    Code:java
    1. public Trade(String a, String b)
    2. {
    3. this.a = a;
    4. this.b = b;
    5. PlayerManager.getExactPlayer(a).openInventory(this.tradeInv.getInventory());
    6. PlayerManager.getExactPlayer(b).openInventory(this.tradeInv.getInventory());
    7. }


    So no, I am not pulling the magical trade out of the air.

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

    TheWolfBadger

    SparkWings Why don't you post all your code... That way we can help you better hopefully?...
     
  11. Offline

    krazytraynz

    But you are. You're not creating a new instance of Trade; you're creating a null one, hence the NPE.
    Code:java
    1. for(Trade trade = new Trade(a, b);;)
     
    AoH_Ruthless likes this.
  12. Offline

    SparkWings

    TheWolfBadger
    Trades class:

    Code:java
    1. package org.jbltd.dctr_.trades;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.scheduler.BukkitRunnable;
    6.  
    7. public class Trade
    8. {
    9. AuraTrades at;
    10. private String a;
    11. private String b;
    12.  
    13. public Trade(AuraTrades at)
    14. {
    15. this.at = at;
    16. }
    17.  
    18. private boolean bA = false;
    19. private boolean bB = false;
    20. private TradeHolder tradeInv = new TradeHolder();
    21. static boolean trading = false;
    22.  
    23. public Trade(String a, String b)
    24. {
    25. this.a = a;
    26. this.b = b;
    27. PlayerManager.getExactPlayer(a).openInventory(this.tradeInv.getInventory());
    28. PlayerManager.getExactPlayer(b).openInventory(this.tradeInv.getInventory());
    29. }
    30.  
    31. public void doAccept(String name)
    32. {
    33. if (name.equals(this.a))
    34. {
    35. this.tradeInv.PlayerAConfirm();
    36. this.bA = true;
    37. }
    38. if (name.equals(this.b))
    39. {
    40. this.tradeInv.PlayerBConfirm();
    41. this.bB = true;
    42. }
    43. checkTrade();
    44. }
    45.  
    46. public void doDeny(String name)
    47. {
    48. if (name.equals(this.a))
    49. {
    50. this.tradeInv.PlayerAWaiting();
    51. this.bA = false;
    52. }
    53. if (name.equals(this.b))
    54. {
    55. this.tradeInv.PlayerBWaiting();
    56. this.bA = false;
    57. }
    58. }
    59.  
    60. public void setBothDeny()
    61. {
    62. this.tradeInv.PlayerAWaiting();
    63. this.bA = false;
    64. this.tradeInv.PlayerBWaiting();
    65. this.bB = false;
    66. }
    67.  
    68. public void checkTrade()
    69. {
    70. if ((this.bA) && (this.bB))
    71. {
    72. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.a), TradeHolder.Slot.SLOTB);
    73. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.b), TradeHolder.Slot.SLOTA);
    74. this.tradeInv.clearInv();
    75. trading = false;
    76. PlayerManager.getExactPlayer(this.a).closeInventory();
    77. PlayerManager.getExactPlayer(this.b).closeInventory();
    78. PlayerManager.sendStubMessage(this.a, " " + AuraTrades.complete);
    79. PlayerManager.sendStubMessage(this.b, " " + AuraTrades.complete);
    80. TradeManager.delTrade(this);
    81. }
    82. }
    83.  
    84. public void doClose(String name)
    85. {
    86. Player player = Bukkit.getPlayer(this.a);
    87. Player player1 = Bukkit.getPlayer(this.b);
    88. if (name.equals(this.a))
    89. {
    90. PlayerManager.sendStubMessage(this.b, this.a + " " + AuraTrades.isDenied);
    91. PlayerManager.sendStubMessage(this.a, " " + AuraTrades.isDenied);
    92. trading = false;
    93. PlayerManager.getExactPlayer(this.b).closeInventory();
    94. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.a), TradeHolder.Slot.SLOTA);
    95. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.b), TradeHolder.Slot.SLOTB);
    96. TradeManager.delTrade(this);
    97. final Player p1 = player;
    98. final Player p2 = player1;
    99. new BukkitRunnable()
    100. {
    101. @SuppressWarnings("deprecation")
    102. public void run()
    103. {
    104. p1.updateInventory();
    105. p2.updateInventory();
    106. }
    107. }.runTaskLater(AuraTrades.plugin1, 4L);
    108. }
    109. else
    110. {
    111. PlayerManager.sendStubMessage(this.a, this.b + " " + AuraTrades.isDenied);
    112. PlayerManager.sendStubMessage(this.b, " " + AuraTrades.isDenied);
    113. trading = false;
    114. PlayerManager.getExactPlayer(this.a).closeInventory();
    115. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.a), TradeHolder.Slot.SLOTA);
    116. this.tradeInv.givePlayerSlot(PlayerManager.getExactPlayer(this.b), TradeHolder.Slot.SLOTB);
    117. TradeManager.delTrade(this);
    118. final Player p1 = player;
    119. final Player p2 = player1;
    120. new BukkitRunnable()
    121. {
    122. @SuppressWarnings("deprecation")
    123. public void run()
    124. {
    125. p1.updateInventory();
    126. p2.updateInventory();
    127. }
    128. }.runTaskLater(AuraTrades.plugin1, 4L);
    129. }
    130. }
    131.  
    132. public void doShowTrade(String name)
    133. {
    134. PlayerManager.getExactPlayer(name).openInventory(this.tradeInv.getInventory());
    135. }
    136.  
    137. public boolean containsPlayer(String name)
    138. {
    139. if ((this.a.equals(name)) || (this.b.equals(name))) {
    140. return true;
    141. }
    142. return false;
    143. }
    144.  
    145. public TradeHolder getHolder()
    146. {
    147. return this.tradeInv;
    148. }
    149.  
    150. public TradeHolder.Slot getPlayerSlot(String name)
    151. {
    152. if (this.a.equals(name)) {
    153. return TradeHolder.Slot.SLOTA;
    154. }
    155. if (this.b.equals(name)) {
    156. return TradeHolder.Slot.SLOTB;
    157. }
    158. return null;
    159. }
    160. }
    161.  


    Manager Class
    Code:java
    1. private static String stub = ChatColor.GOLD + AuraTrades.stubby + ChatColor.RESET;
    2.  
    3. @SuppressWarnings("null")
    4. public static boolean isPlayerInTrade(String name)
    5. {
    6. Validate.notNull(name, "Name cannot be null");
    7. for(Trade trade = null;;)
    8. if (trade.containsPlayer(name)) {
    9. return true;
    10. }
    11. }
    12.  
    13. @SuppressWarnings("null")
    14. public static boolean isPlayerInvitedToTrade(String name)
    15. {
    16. Validate.notNull(name, "Name cannot be null");
    17. for(Invite invite = null;;)
    18.  
    19. if (invite.containsPlayer(name)) {
    20. return true;
    21. }
    22. }
    23.  
    24. public static void sendStubMessage(String p, String m)
    25. {
    26. sendMessage(p, stub + m);
    27. }
    28.  
    29. public static void sendMessage(String name, String m)
    30. {
    31. Validate.notNull(name, "Name cannot be null");
    32. for(Player player : Bukkit.getOnlinePlayers())
    33. if (player.getName().equals(name))
    34. {
    35. player.sendMessage(m);
    36. return;
    37. }
    38. }
    39.  
    40.  
    41. public static Player getPlayer(String name)
    42. {
    43. Validate.notNull(name, "Name cannot be null");
    44. String lname = name.toLowerCase();
    45.  
    46. for(Player player : Bukkit.getOnlinePlayers())
    47. if (player.getName().equalsIgnoreCase(lname)) {
    48. return player;
    49. }
    50. return null;
    51. }
    52.  
    53. public static Player getExactPlayer(String name)
    54. {
    55. Validate.notNull(name, "Name cannot be null");
    56. String lname = name.toLowerCase();
    57.  
    58. for(Player player : Bukkit.getOnlinePlayers())
    59. if (player.getName().equalsIgnoreCase(lname)) {
    60. return player;
    61. }
    62. return null;
    63. }
    64.  
     
  13. Offline

    1Rogue

    And none of that matters if you don't have any instance to use.
     
  14. Offline

    Unknowncmbk

    Oh that doesn't look like a typo? Come on man, tired of the p*ssing contests that happen on these forums.
     
  15. SparkWings
    So this doesn't really work.

    Code:java
    1. for(Trade trade = null;;)

    http://www.homeandlearn.co.uk/java/java_for_loops.html
    There is a for loop tutorial.

    Also, you are setting the variable trade to be null. Then trying to use it. You CANNOT use something that is set to null.
    Setting a number to 0 then iterating it +1 each time in the for loop is NOT the same as what you are doing here.

    Edit: Also, this doesn't work either.
    Code:java
    1. for(Invite invite = null;;)
     
  16. Offline

    RainoBoy97

    For your first question, this does the exact same thing:
    Code:
    Bukkit.getPlayerExact("name");
    
     
  17. Offline

    xTrollxDudex

    It does, Necrodoom wants to bring your attention to it.
     
  18. Offline

    SparkWings

    Okay, I removed all the for statements, but still getting the NPE because of the null objects. Can someone help me as to how to not reference a null object? Sorry if I seem like a noob in asking all these dumb questions.
     
  19. Offline

    es359


    You need to instantiate the object(s). SparkWings
     
Thread Status:
Not open for further replies.

Share This Page