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("");
    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();



    }, 500, 100);

    [Server thread/WARN]: [Refill] Task #65 for Refill v1.1 generated an exception
    at$ ~[?:?]
    at ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at [spigot.jar:git-Spigot-db6de12-18fbb24]
    at Source) [?:1.8.0_131]
  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 ""

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

    I'm sure you meant to do something like Bukkit.getWorld(getConfig().getString(""));
  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 ""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. :)