Solved location.getBlock is null?

Discussion in 'Spigot Plugin Development' started by Hex_27, Apr 24, 2017.

  1. Does it mean the world is null or something?
    Part of Code, with "<<<<<<<<" pointing at source
    Code (Text):
            private void initStructure(final Land land) {
                new BukkitRunnable(){
                    public void run() {
                        Structure structure = land.getStructure();
                        if(structure == null) return;
                        if(structure.getLoc() == null) return;
                        if(structure.getLoc().toLocation() == null) return;
                        if(land.getOwner() == null){
                            Block block = structure.getLoc().toLocation().getBlock(); //<<<<<<<<<
    Code (Text):
    [21:11:40 WARN]: [Kingdoms] Task #577 for Kingdoms v13.2.4 generated an exception
            at org.bukkit.Location.getBlock( ~[spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at$SyncLoadTask$ ~[?:?]
            at ~[spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat( [spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D( [spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D( [spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C( [spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at [spigot-1.10.2.jar:git-Spigot-72c2605-251a5b6]
            at Source) [?:1.8.0_111]
  2. Have you tried debugging??
  3. What do I debug in this case?
  4. So it doesn't return when you call structure.getLoc() so I'm assuming that's not what's null? Or does it? Because it wouldn't print that npe if it does because it returns
  5. Have you tried using #getBlockX() ?
  6. Uh.... what's the relevance about it here?
  7. A wild guess here, but try checking if the Location's 'World' is null?
  8. Try debugging it.
    Check if land is null, after that structure, world, location the finally block.
  9. All of those are already checked if null, except world. Will try the world thing soon

  10. Have u tried printing out the location. If the location is null, the block will also technically be null.
  11. Code (Java):
    if(structure.getLoc() == null) return;
    if(structure.getLoc().toLocation() == null) return;
  12. I think if the block is null, it means it's air.
  13. Choco


    The World object in Location is most certainly null. The above line is as followed:
    Code (Java):
    return world.getBlockAt(this);
    The only thing that can possibly be null is "world". The constructor for Location does not check if the provided World is null, so it is very likely that you accidentally retrieved a World by name that did not exist.

    Did no one else bother to take a look at Bukkit's source code? You know... the line that was outlined in the stacktrace? A lot of the answers in this thread are absurd and are just wild guesses. Please do a bit of research before actually replying. This thread could have been answered just under 2 hours ago
    • Agree Agree x 1
    • Useful Useful x 1
  14. Ah sorry I had not noticed that.
    Anyway, I went throught the Spigot source and checked the getBlock method.
    Code (Text):
    public Block getBlock() {
            return world.getBlockAt(this);
    So...... the only possible issue is, the world in the aquired Location object does not exist.
    EDIT: lol a guy beat me to this. RIP
    • Like Like x 1
  15. Ok, thanks, I'll mark as solved. Anyway, where did you guys find the source? The links I google led me to the javadoc
  16. Choco


    All source code can be found on SpigotMC's Stash repository. This contains source code for CraftBukkit, Bukkit and Spigot, as well as various other projects such as plugin annotations and fernflower
    • Like Like x 1
    • Optimistic Optimistic x 1