Solved NPE from getType()

Discussion in 'Plugin Development' started by TechNotes, Jun 19, 2014.

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

    TechNotes

    When I execute /materials it iterates through every block within the two coordinates I specify and adds the Material of every block it finds to a HashMap.

    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("materials")) {
    2. if (sender instanceof Player) {
    3. Player player = (Player) sender;
    4.  
    5. Integer[] corner1 = { -400, 256, -70 };
    6. Integer[] corner2 = { -409, 5, -61 };
    7.  
    8. HashMap<Material, Integer> materialMap = analyzeShip(corner1,
    9. corner2);
    10.  
    11. player.sendMessage(materialMap.toString());
    12.  
    13. return true;
    14. }


    And the analyzeShip(Integer[], Integer[]) method:

    Code:java
    1. public HashMap<Material, Integer> analyzeShip(Integer[] corner1,
    2. Integer[] corner2) {
    3. HashMap<Material, Integer> materialMap = new HashMap<Material, Integer>();
    4.  
    5. /*This just makes sure that corner1[] has all the least coordinates, and corner2[] has the greatest,
    6. so it makes my coding below easier*/
    7.  
    8. for (int index = 0; index < 3; index++)
    9. if (corner1[index] > corner2[index]) {
    10. int temp = corner1[index];
    11. corner1[index] = corner2[index];
    12. corner2[index] = temp;
    13.  
    14. }
    15.  
    16. for (int x = corner1[0]; x <= corner2[0]; x++) {
    17. for (int y = corner1[1]; y <= corner2[1]; y++) {
    18. for (int z = corner1[2]; z <= corner2[2]; z++) {
    19.  
    20. //Debug statement, with an output of null, as expected from the NPE
    21.  
    22. System.out.println(new Location(player.getWorld(), x, y, z)
    23. .getBlock().getType());
    24.  
    25. Material m = new Location(player.getWorld(), x, y, z)
    26. .getBlock().getType();
    27.  
    28. if (m == Material.AIR || m == Material.BEDROCK) {
    29. continue;
    30.  
    31.  
    32. }
    33. if (!materialMap.containsKey(m)) {
    34. materialMap.put(m, 1);
    35. } else {
    36. materialMap.put(m, materialMap.get(m) + 1);
    37. }
    38.  
    39. }
    40. }
    41. }
    42.  
    43. return materialMap;
    44. }



    I get a NullPointerException on my debug statement in the analyzeShip method:

    Code:java
    1. System.out.println(new Location(player.getWorld(), x, y, z)
    2. .getBlock().getType());


    And before I inserted that debug, it was on the line:

    Code:java
    1. Material m = new Location(player.getWorld(), x, y, z)
    2. .getBlock().getType();


    I've never had this issue before; does anyone know what the problem is? Thanks

    Sorry to do this, but bump. Anyone know what the problem is?

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

    _Filip

    I am not sure, but I think it may have to do with getBlock() returning null if there is no block there.
    Just make a check such as
    Code:java
    1. Block block;
    2. if (((block = new Location(player.getWorld(), x, y, z).getBlock()) != null){
    3. //INSERT CODE
    4. }


    Then make
    Code:java
    1. Material m = block.getType();
     
  3. Offline

    TechNotes

    That's a good idea, but I already know if there were no block there, it would return air.
     
  4. Offline

    TheHandfish

    Can I see the full stack-trace/error?
     
  5. Offline

    TechNotes

    TheHandfish

    Code:
    TechNotes issued server command: /materials
    org.bukkit.command.CommandException: Unhandled exception executing command 'materials' in plugin BlockLaunch v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at org.bukkit.craftbukkit.v1_7_R2.CraftServer.dispatchCommand(CraftServer.java:696) ~[craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.PlayerConnection.handleCommand(PlayerConnection.java:952) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:147) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.ServerConnection.c(SourceFile:134) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:657) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:250) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:548) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:459) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
    Caused by: java.lang.NullPointerException
        at com.blocklaunch.blocklaunch.BlockLaunch.analyzeShip(BlockLaunch.java:158) ~[?:?]
        at com.blocklaunch.blocklaunch.BlockLaunch.onCommand(BlockLaunch.java:86) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.7.5-R0.1-20140329.215848-10.jar:git-Bukkit-1.7.2-R0.3-10-gcf5a547-b3035jnks]
        ... 13 more
    
    Hope it helps, and thanks!
     
  6. Offline

    AmShaegar

    You need to check every possibility for beeing null. Just to see where the problem comes from. I's suggeest something like this:
    Code:
    System.out.println(player);
    System.out.println(new Location(player.getWorld(), x, y, z));
    System.out.println(new Location(player.getWorld(), x, y, z).getBlock());
    If you see "null" in console then you know which line causes it an thus which expression results in null.
     
  7. Offline

    TechNotes

    AmShaegar
    Good idea, don't know why I didn't think of that.

    So i did:

    Code:java
    1. System.out.println(x);
    2. System.out.println(y);
    3. System.out.println(z);
    4.  
    5. System.out.println(new Location(player.getWorld(), x, y, z)
    6. .toString());


    and it returned the correct integers, but with the location.toString(), it returned null, which means it can't get the World... any ideas?
     
  8. Offline

    AmShaegar

    what about player, player.getWorld() and why did you add .toString? Don't do that. println() takes an Object as argument and will invoke toString() on its own if necessary. O=therwise it will print "null" without crashing.
     
  9. Offline

    TechNotes

    AmShaegar

    Wow, talk about embarrassed! I only declared my player object, I didn't initialize it! :( Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page