Solved Null pointer when finding block.

Discussion in 'Spigot Plugin Development' started by black_, Jun 2, 2017.

  1. Hi! I am getting a null pointer exception when looping through an area checking for chest blocks. In earlier stages of this plugin, I never saw this error, so I am quite puzzled as to what is causing it. Thanks for any help!

    Line 193 is the "Block block = world.getBlockAt(x, y, z);" btw
    count = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    public void run() {

    World world = Bukkit.getWorld("sel.world");
    int x1 = getConfig().getInt("sel.x1");
    int x2 = getConfig().getInt("sel.x2");
    int y1 = getConfig().getInt("sel.y1");
    int y2 = getConfig().getInt("sel.y2");
    int z1 = getConfig().getInt("sel.z1");
    int z2 = getConfig().getInt("sel.z2");


    for (int x = x1; x <= x2; x++) {
    for (int y = y1; y <= y2; y++) {
    for (int z = z1; z <= z2; z++) {

    Block block = world.getBlockAt(x, y, z);

    if (block.getType() == Material.CHEST) {

    Chest c = (Chest) block.getState();

    Inventory inventory = c.getInventory();

    chestRefill(inventory);

    }
    }
    }
    }

    }
    }, 500, 100);

    [Server thread/WARN]: [Refill] Task #65 for Refill v1.1 generated an exception
    java.lang.NullPointerException
    at me.black.refill.Main$1.run(Main.java:193) ~[?:?]
    at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    [00:37:42]
     
  2. A stacktrace would be beneficial, and line numbers to that code to compare to the stacktrace..
     
  3. Try checking if there is a block there first.

    if (block != null) {

    }
     
  4. added sorry about that.
     
  5. world is null. Means there is no world named "sel.world"

    For future reference, an NPE gets thrown when you try to use something on a null object:
    Code (Text):
    null.doSomething();
    where doSomething is throwing the NPE because it doesnt exist on a null Object.
    Code (Text):
    world.getBlockAt(x,y,z);
    getBlockAt is throwing the NPE, meaning world is null.
    ;)

    I'm sure you meant to do something like Bukkit.getWorld(getConfig().getString("sel.world"));
     
  6. Tried that, does not work.
     
  7. I always do
    Location l : new Location(world,x,y,z);
    Block b : l.getBlock();
     
  8. I feel so stupid. I forgot to get "sel.world"from the config, not from bukkit. I know what an NPE means, but due to my blindness, I couldn't see what was in plain sight. I appreciate the help!:)
     
    • Like Like x 1
  9. Edited my post above to show the thought process in finding the problem to these kinds of things, even for the blind ;)
     
  10. Thanks I appreciate it. :)