Null Pointer Exception

Discussion in 'Plugin Development' started by CodenameTitan, Sep 27, 2013.

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

    CodenameTitan

    Hello,
    I am having a NPE problem at line 58 of my arena manager code. Any help would be good!:

    Code:java
    1. package me.codenametitan.pbpt.arena;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7.  
    8. import me.codenametitan.pbpt.PBPTMain;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.Location;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.ItemStack;
    14.  
    15. public class ArenaManager{
    16.  
    17. private static PBPTMain plugin;
    18. public ArenaManager(PBPTMain plugin) {
    19. ArenaManager.plugin = plugin;
    20. }
    21.  
    22.  
    23. public Map<String, Location> locs = new HashMap<String, Location>();
    24.  
    25. public static ArenaManager a = new ArenaManager(plugin);
    26.  
    27. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    28. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    29.  
    30. List<Arena> arenas = new ArrayList<Arena>();
    31.  
    32.  
    33. public static ArenaManager getManager(){
    34. return a;
    35. }
    36.  
    37. //get an Arena object from the list
    38. public Arena getArena(int i){
    39. for(Arena a : arenas){
    40. if(a.getId() == i){
    41. return a;
    42. }
    43. }
    44. return null;
    45. }
    46.  
    47.  
    48. public void addPlayer(Player p, int i){
    49. Arena a = getArena(i);
    50. if(a == null){
    51. p.sendMessage("Invalid arena!");
    52. return;
    53. }
    54.  
    55. a.getPlayers().add(p.getName());
    56. inv.put(p.getName(), p.getInventory().getContents());
    57. armor.put(p.getName(), p.getInventory().getArmorContents());
    58. p.teleport(a.spawn);//THIS LINE IT OCCURUS
    59. }
    60.  
    61.  
    62. public void removePlayer(Player p){
    63. Arena a = null;
    64. for(Arena arena : arenas){
    65. if(arena.getPlayers().contains(p.getName())){
    66. a = arena;
    67. }
    68.  
    69. }
    70. if(a == null || !a.getPlayers().contains(p.getName())){
    71. p.sendMessage("Invalid operation!");
    72. return;
    73. }
    74.  
    75. a.getPlayers().remove(p.getName());
    76. p.getInventory().setContents(inv.get(p.getName()));
    77. p.getInventory().setArmorContents(armor.get(p.getName()));
    78.  
    79. inv.remove(p.getName());
    80. armor.remove(p.getName());
    81. p.teleport(locs.get(p.getName()));
    82. locs.remove(p.getName());
    83. }
    84.  
    85.  
    86. public void createArena(Location l){
    87. int num = arenas.size() + 1;
    88.  
    89. Arena a = new Arena(l, num);
    90. arenas.add(a);
    91.  
    92. plugin.getConfig().set("Arenas" + num, serializeLoc(l));
    93. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    94. list.add(num);
    95. plugin.getConfig().set("Arenas.Arenas", list);
    96. plugin.saveConfig();
    97. }
    98.  
    99. public void removeArena(int i) {
    100. for(String s : getArena(i).getPlayers()) {
    101. Player p = Bukkit.getPlayerExact(s);
    102. if(p != null) {
    103. p.teleport(locs.get(p.getName()));
    104. locs.remove(p.getName());
    105. }
    106. }
    107. }
    108. public boolean isInGame(Player p) {
    109. for(Arena a : arenas) {
    110. if(a.getPlayers().contains(p.getName())) {
    111. return true;
    112. }
    113.  
    114. }
    115. return false;
    116. }
    117.  
    118. public void loadGames() {
    119. if(plugin.getConfig().getIntegerList("Arenas.Arenas").isEmpty()) {
    120. return;
    121. }
    122.  
    123. for(int i : plugin.getConfig().getIntegerList("Arenas.Arenas")) {
    124. Arena a = new Arena(deserializeLoc(plugin.getConfig().getString("Arenas" + i)), i);
    125. arenas.add(a);
    126. }
    127. }
    128.  
    129. public String serializeLoc(Location l) {
    130. return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
    131. }
    132.  
    133. public Location deserializeLoc(String s) {
    134. String[] st = s.split(",");
    135. return new Location(Bukkit.getWorld(st[1]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    136. }
    137.  
    138. }
    139.  
     
  2. Offline

    nuclearmissile

    You're using the name "a" for both the ArenaManager object at the top and the Arena objects farther down in the code. Could that be causing it?

    Sorry if it's not the answer you need, I'm not professional enough to instantly spot any incorrect code :3
     
  3. Offline

    CodenameTitan

    nuclearmissile I don't think thats the issue because the Arena object is a local variable
     
  4. Offline

    1Achmed1

    I believe it's because that code will run if the arena is null or not. It should be in an else if.
    To say it another way, it will check that the arena exists. If not it will return and move on. It's moving on to the teleport code, so if the arena doesn't exist it'll still try to to them.
     
  5. Offline

    nisovin

    It won't move on, because it returns from the function.

    My guess is a.spawn is null.
     
  6. Offline

    1Achmed1

    Last time I checked, void methods don't return...
     
  7. Offline

    amhokies

    Yeah, but the method isn't actually trying to return anything. Doing "return;" in a void method exits out of the method without returning anything.
     
    1Achmed1 likes this.
  8. Offline

    xTrollxDudex

    I'm not sure you made that on your own

    Otherwise, there are a few possibilities:
    One, you didn't fill the list
    Two, the number is not within the lsit
    Three, you didn't set the spawn
     
  9. Offline

    1Achmed1

    Or 4:
    Further more, if you make the method a boolean instead of void, your return should work, as long as you return a value. In your case, false.

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

    xTrollxDudex

    return; escapes the method, prevents the method from executing code below it... That can't possibly cause a NPE
     
  11. Offline

    1Achmed1

    void can't return.
     
  12. A full stack trace would help here. Looking at you code, for me the given line can't cause a NPE. Reason: a is checked for being null, so it can't be null, p is used in the lines before line 58, so it can't be null either, or the NPE is on a different line. If this line really appears in the stack trace, only a.spawn can be null as nisovin said, or there is some other circumstance not related to the parameters used in the call.
     
  13. Offline

    Quantix

    No, you absolutely can and are expected to use the return keyword in a void method. Void's don't return actual values, hence there being no value (such as a boolean, null or object) after the return keyword. The return keyword in java is often used in void methods as an alternative to using many nested conditional statements in a void method by exiting the block of code and not executing any code below.
     
  14. Offline

    1Achmed1

    Then why do I get an error when I return?...
    Plus, it should automatically infer when you return, typically at the end of if statements, at the end of the method, and in try/catch/finally statements
     
  15. Offline

    CodenameTitan

    xTrollxDudex Sorry dude. I used your code. It does the same if I use your source
     
  16. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page