NullPointerException on Block.setType() :D

Discussion in 'Plugin Development' started by Samkio, Sep 18, 2011.

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

    Samkio

    Greetings.
    Currently working on the CityGen for RPGWorld.
    A column of air is used in it's creation. On CHUNK_LOAD priority low.
    However i get a nullpointerexception when trying to set a block type.

    StackTrace:
    Show Spoiler


    Code:
    19:49:08 [SEVERE] Could not pass event CHUNK_LOAD to RPGWorld
    java.lang.NullPointerException
            at net.minecraft.server.WorldServer.c(WorldServer.java:106)
            at net.minecraft.server.World.addEntity(World.java:880)
            at net.minecraft.server.World.addEntity(World.java:839)
            at net.minecraft.server.Block.a(Block.java:319)
            at net.minecraft.server.Block.dropNaturally(Block.java:303)
            at net.minecraft.server.Block.g(Block.java:290)
            at net.minecraft.server.BlockFlower.g(SourceFile:37)
            at net.minecraft.server.BlockFlower.doPhysics(SourceFile:28)
            at net.minecraft.server.World.k(World.java:459)
            at net.minecraft.server.World.applyPhysics(World.java:437)
            at net.minecraft.server.World.update(World.java:407)
            at net.minecraft.server.World.setTypeId(World.java:379)
            at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:77)
    
            at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:82)
    
            at me.samkio.RPGWorld.CityGen.CityToolbox.makeCylinderLayers(CityToolbox
    .java:57)
            at me.samkio.RPGWorld.CityGen.CityToolbox.makeCylinder(CityToolbox.java:
    15)
            at me.samkio.RPGWorld.CityGen.City.construct(City.java:146)
            at me.samkio.RPGWorld.RPGWWorldListener.onChunkLoad(RPGWWorldListener.ja
    va:29)
            at org.bukkit.plugin.java.JavaPluginLoader$48.execute(JavaPluginLoader.j
    ava:577)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:338)
            at net.minecraft.server.ChunkProviderServer.getChunkAt(ChunkProviderServ
    er.java:90)
            at net.minecraft.server.ChunkProviderServer.getOrCreateChunk(ChunkProvid
    erServer.java:118)
            at net.minecraft.server.World.getChunkAt(World.java:280)
            at net.minecraft.server.World.getTypeId(World.java:227)
            at net.minecraft.server.World.isEmpty(World.java:231)
            at net.minecraft.server.World.a(World.java:198)
            at net.minecraft.server.WorldProvider.canSpawn(SourceFile:51)
            at net.minecraft.server.World.canSpawn(World.java:98)
            at net.minecraft.server.World.c(World.java:186)
            at net.minecraft.server.World.<init>(World.java:148)
            at net.minecraft.server.WorldServer.<init>(WorldServer.java:27)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:181)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:149)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:337)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    19:49:09 [INFO] Preparing start region for level 0 (Seed: -5470134110124221147)


    Code Snippet:

    Code:
                        LoadTheDamnChunk(new Location(l.getWorld(), bx + x, by, bz
                                + y));
                        LoadTheDamnChunk(new Location(l.getWorld(), bx + x, by, bz
                                - y));
                        LoadTheDamnChunk(new Location(l.getWorld(), bx - x, by, bz
                                + y));
                        LoadTheDamnChunk(new Location(l.getWorld(), bx - x, by, bz
                                - y));
                        if (l.getWorld().getBlockAt(bx + x, by, bz +  y).getType() != Material.AIR && l.getWorld().getBlockAt(bx + x,  by, bz + y).getType() != null) {
                            l.getWorld().getBlockAt(bx + x, by, bz + y)
                            .setTypeId(0, true);
                        }
                        if (l.getWorld().getBlockAt(bx + x, by, bz -  y).getType() != Material.AIR && l.getWorld().getBlockAt(bx + x,  by, bz - y).getType() != null) {
                            l.getWorld().getBlockAt(bx + x, by, bz - y)
    
                            .setTypeId(0, true);
                        }
                        if (l.getWorld().getBlockAt(bx - x, by, bz +  y).getType() != Material.AIR && l.getWorld().getBlockAt(bx - x,  by, bz + y).getType() != null ) {
                            l.getWorld().getBlockAt(bx - x, by, bz + y)
                                    .setTypeId(0, true);  //<--- Line 57 :)
                            if (l.getWorld().getBlockAt(bx - x, by, bz - y)
                                    .getType() != Material.AIR &&  l.getWorld().getBlockAt(bx - x, by, bz - y).getType() != null) {
                                l.getWorld().getBlockAt(bx - x, by, bz - y).setTypeId(0, true);
                            }
    
                        }

    As you can see there is a lot of stuff there that i have tried to work around the problem! xD
    I thought it was the fact that chunks are loaded so i added a function that did so.
    Code:
        private void LoadTheDamnChunk(Location l) {
            World w = l.getWorld();
            Chunk c = w.getChunkAt(l);
            if (!w.isChunkLoaded(c)) {
                w.loadChunk(c);
            }
        }

    If anyone can help me i will be very grateful! Been doing my head in.
    Build 1060 btw.
    Regards
    Samkio
     
  2. Offline

    DrAgonmoray

    I might not know what I'm talking about here, but I think I had a similar problem in the past.

    I _think_ that Material.AIR returns null, or something like that. :O
     
  3. Offline

    Samkio

    Hmm i'll try using ids. and see how it goes.

    Edit nope didn't work :p
    It's on the setBlockType :/ The block must be null but it isn't.
     
  4. Offline

    Numenorean95

    Material.AIR is nasty, maybe just check if its null? Maybe also as a test try checking whether it is anything but air, it might shed some light.
     
  5. Offline

    DrAgonmoray

    Hmm.. Try adding some println()'s to locate the error. For example
    Code:
    System.out.println(l.getWorld().getBlockAt(bx + x, by, bz +  y).getType());
     
  6. Offline

    Samkio

    @DrAgonmoray
    Okay will try gonna take a while xD
    Slowly getting there... :p Lots of STONE, AIR and DIRT. as suspected.
    EDIT: Okay so i made it print the material types of the blocks when an error occupred and none of them are null.
    However all of them included at least 1 instance of air. Not sure if this is just coincidence.

    @Numenorean95
    Yeah i removed all instance of Material.AIR
    l.getWorld().getBlockAt(bx - x, by, bz + y).getTypeId() != 0 //If it is anything but air. :p
     
  7. Offline

    dkramer

    It should automatically load the chunk if you call for a block that's not loaded. This is what I would do.

    Code:
    Block b1 = l.getWorld().getBlockAt(bx + x, by, bz + y);
    Block b2 = l.getWorld().getBlockAt(bx + x, by, bz - y);
    Block b3 = l.getWorld().getBlockAt(bx - x, by, bz + y);
    Block b4 = l.getWorld().getBlockAt(bx - x, by, bz - y);
    
    
    if (b1.getTypeId()!= null && b1.getTypeId() != 0) {
        b1.setTypeId(0, true);
    }
    
    
    if (b2.getTypeId()!= null && b2.getTypeId() != 0) {
        b2.setTypeId(0, true);
    }
    
    
    if (b3.getTypeId()!= null && b3.getTypeId() != 0) {
        b3.setTypeId(0, true);
    
        if (b4.getTypeId()!= null && b4.getType() != 0) {
            b4.setTypeId(0, true);
        }
    
    }
     
  8. Offline

    Samkio

    @dkramer
    Thanks for the code.
    I tried it but same error.
    A nullPointerException on b3.setTypeId(o,true);
    Which is funny as the block type is not null as checked before.
     
  9. Offline

    dkramer

    @Samkio
    hmm... maybe try
    if(!b3.isEmpty() && b3.getTypeId()!= null && b3.getTypeId() != 0)
     
  10. Offline

    Samkio

    @dkramer
    Tried that and no luck :/
    Same error on thhe setTypeId
     
  11. Offline

    dkramer

    That's very odd... don't know what to tell you :(
     
  12. Offline

    Samkio

    :/ sddddgjd suggested that i add all the "bad blocks" to an array and then set them to air after.
    I'll see if that works.

    Okay issue resolved by action stated above! :D
    Not sure why offsetting the block change works... but it does xD
    Thanks for all the help :)

    Now to solve notch puddles xD
    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  13. Offline

    dkramer

    @Samkio You could create a delayed task if all else fails :D
     
  14. Offline

    Samkio

    I think that's what i'm going to have to do xD
    I'll give it a go when i get to my pc. :p
     
  15. Offline

    wachnlurn

    Hi i just stumbled upon this thread. I'm wondering if you are talking about the same thing that i posted about a few weeks ago. This is the thread. I am not fluent in code but from what i gather maybe you have found a fix for the chunks reporting as air? If so can this be turned into a plugin of some sorts? My old world is filled with these chunks, and i have been unable to figure out what causes it or a fix for it. If nothing else is there a way to prevent it from happening or is it just random?
    More recently ive been trying to WorldEdit old structures to the new 1.8 world and some have these 'air chunks' and upon pasting have massive holes, very annoying for me and for players : (
     
  16. Offline

    Samkio

    @wachnlurn
    Nah i think the error was the i was trying to change block types that hadn't been truly loaded yet.

    These "airchunk" probably occur when copying and pasting. Because that particular chunk when copying and pasting may not be loaded.
    I would use a program like MCEdit to copy large schematics across.
     
  17. Offline

    Numenorean95

    Not that it matters, but i meant to try literally checking for true on every single block except air ;)
     
  18. Offline

    Samkio

Thread Status:
Not open for further replies.

Share This Page