NPE while teleporting to a location stored in HashMap

Discussion in 'Plugin Development' started by billofbong, Dec 28, 2011.

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

    billofbong

    O hai, I'm billofbong.

    I have almost finished a plugin, but I have one single NPE holding me back... I'll add a comment on where it happens:
    Code:java
    1. package org.awesomecraft.plugins.orphanage;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.World;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Orphanage extends JavaPlugin {
    15. Map<World, Location> orphanage = new HashMap<World, Location>();
    16. Map<Player, Player> mothers = new HashMap<Player, Player>(); //<Mother, Orphan>
    17. Map<Player, Boolean> isOrphan = new HashMap<Player, Boolean>(); //<Orphan, isOrphan>
    18. public void onDisable() {
    19. System.out.println(this + " is now disabled!");
    20. try {
    21. SLAPI.save(mothers, "mothers.map");
    22. } catch (Exception ex) {
    23. }
    24. try {
    25. SLAPI.save(isOrphan, "orphans.map");
    26. } catch (Exception ex) {
    27. }
    28. try {
    29. SLAPI.save(orphanage, "orphanage.map");
    30. }
    31. catch(Exception ex) {
    32. }
    33. }
    34.  
    35. public void onEnable() {
    36. getCommand("adopt").setExecutor(new CommandExecutor() {
    37.  
    38. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    39. if(args.length == 1) {
    40. if(cs instanceof Player) {
    41. Player target = getServer().getPlayer(args[0]);
    42. Player sender = (Player) cs;
    43. sender.sendMessage(ChatColor.GREEN + "You are now the parent of " + target.getDisplayName() + "!");
    44. target.sendMessage(ChatColor.GREEN + "You have been adopted by " + sender.getDisplayName() + "!");
    45. mothers.put(sender, target);
    46. isOrphan.put(target, Boolean.FALSE);
    47. }
    48. else{
    49. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    50. }
    51. }
    52. return true;
    53. }
    54. });
    55. getCommand("abandon").setExecutor(new CommandExecutor() {
    56.  
    57. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    58. if(cs instanceof Player) {
    59. Player target = getServer().getPlayer(args[0]);
    60. Player sender = (Player) cs;
    61. if(mothers.containsValue(target) && mothers.containsKey(sender)) {
    62. mothers.put(null, null);
    63. isOrphan.put(target, true);
    64. target.sendMessage(ChatColor.RED + "You are an orphan again.");
    65. sender.sendMessage(ChatColor.RED + "You abandoned you orphan on the streets.");
    66. } else {
    67. sender.sendMessage(ChatColor.RED + "You aren't the parent of that person!");
    68. }
    69. }
    70. return true;
    71. }});
    72. getCommand("setorphanage").setExecutor(new CommandExecutor() {
    73.  
    74. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    75. if(cs instanceof Player) {
    76. Player sender = (Player) cs;
    77. orphanage.put(sender.getWorld(), sender.getLocation());
    78. sender.sendMessage(ChatColor.GREEN + "Set an orphanage at you current location.");
    79. }
    80. else {
    81. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    82. }
    83. return true;
    84. }
    85. });
    86. getCommand("orphanage").setExecutor(new CommandExecutor() {
    87.  
    88. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    89. if(cs instanceof Player) {
    90. Player sender = (Player) cs;
    91. if(orphanage != null) {
    92. Location tele = orphanage.get(sender.getWorld()); //right here...
    93. sender.teleport(tele);
    94. }
    95. else {
    96. sender.sendMessage(ChatColor.RED + "An orphanage hasn't been set yet.");
    97. }
    98. }
    99. else {
    100. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    101. }
    102. return true;
    103. }
    104. });
    105. try {
    106. mothers = (Map<Player, Player>)SLAPI.load("mothers.map");
    107. } catch (Exception ex) {
    108. }
    109. try {
    110. isOrphan = (Map<Player, Boolean>)SLAPI.load("orphans.map");
    111. } catch (Exception ex) {
    112. }
    113. try {
    114. orphanage = (Map<World, Location>)SLAPI.load("orphanage.map");
    115. } catch (Exception ex) {
    116. }
    117.  
    118. System.out.println(this + " is now enabled!");
    119. }
    120. }
    121.  

    This happens after setting an orphanage, reloading, and then trying to do /orphanage (which teleports you to the orphanage).
    Here's the error:
    Code:
    13:18:24 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'orphanage' in plugin Orphanage v1.0
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:165)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:378)
    	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:757)
    	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:722)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:715)
    	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:93)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:527)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.NullPointerException
    	at org.bukkit.craftbukkit.entity.CraftPlayer.teleport(CraftPlayer.java:308)
    	at org.bukkit.craftbukkit.entity.CraftEntity.teleport(CraftEntity.java:156)
    	at org.awesomecraft.plugins.orphanage.Orphanage$4.onCommand(Orphanage.java:97)
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    	... 12 more
    Many thanks,

    billofbong
     
  2. Offline

    Nitnelave

    You doing the wrong check... Orphanage is never null, but orphanage.get(sender.getWorld()) is, because this orphanage hasn't been set yet. Either you define tele then check for nullity, or you check for orphanage.containsKey(sender.getWorld)
     
Thread Status:
Not open for further replies.

Share This Page