setType(Material.AIR); NullPointerException

Discussion in 'Plugin Development' started by vildaberper, Apr 8, 2011.

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

    vildaberper

    I sometimes get this:
    Code:
    17:26:30 [ALLVARLIG] java.lang.NullPointerException
    17:26:30 [ALLVARLIG]    at net.minecraft.server.World.f(World.java:1293)
    17:26:30 [ALLVARLIG]    at net.minecraft.server.MinecraftServer.h(MinecraftServe
    r.java:361)
    17:26:30 [ALLVARLIG]    at net.minecraft.server.MinecraftServer.run(MinecraftSer
    ver.java:283)
    17:26:30 [ALLVARLIG]    at net.minecraft.server.ThreadServerApplication.run(Sour
    ceFile:375)
    17:26:30 [ALLVARLIG] Unexpected exception
    java.lang.NullPointerException
            at net.minecraft.server.World.f(World.java:1293)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:361)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:283)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:375)
    
    with this code:
    Code:
    public static int clearConnectedBlocks(Block block){
    			BlockFace bf = null;
    			int count = 0;
    
    			unchecked.clear();
    			unchecked.add(block);
    			while(unchecked.size() > 0){
    				for(int i = 0; i < 6; i++){
    					if(i == 0){
    						bf = BlockFace.DOWN;
    					}else if(i == 1){
    						bf = BlockFace.EAST;
    					}else if(i == 2){
    						bf = BlockFace.NORTH;
    					}else if(i == 3){
    						bf = BlockFace.SOUTH;
    					}else if(i == 4){
    						bf = BlockFace.UP;
    					}else if(i == 5){
    						bf = BlockFace.WEST;
    					}
    					if(!unchecked.get(0).getFace(bf).getType().equals(Material.AIR) && !isUnchecked(unchecked.get(0).getFace(bf))){
    						unchecked.add(unchecked.get(0).getFace(bf));
    					}
    				}
    #				unchecked.get(0).setType(Material.AIR);
    				count++;
    				unchecked.remove(0);
    				try {
    					Thread.sleep(10);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    			return count;
    		}
    
    The marked row is where the exception leads me to.

    I call this function with this code:
    Code:
    this.taskId = this.getServer().getScheduler().scheduleAsyncDelayedTask(
    						this,
    						new Runnable(){
    							@Override
    							public void run(){
    								rfplayer.sendMessage("Cleared " + Con.clearConnectedBlocks(rfplayer.getTargetBlock(null, 100)) + " blocks.");
    							}
    						}
    				);
    
    What is going on here?
     
  2. Offline

    MrChick

    rfplayer is null.
     
  3. Offline

    vildaberper

    Nope, its not that.
    And note that it works perfectly sometimes.

    It would point to this row if rfplayer was null:
    Code:
    unchecked.add(block);
    
     
  4. Offline

    MrChick

    rfplayer HAS to be null. you don't have any variable called rfplayer in your anonymous class implementing runnable.
     
  5. Offline

    vildaberper

    rfplayer is not null. If it was, why would it only work sometimes?
    Heres the complete source:
    http://pastebin.com/k5JSKnH9
     
  6. Offline

    MrChick

    rfplayer IS null, at least inside of your runnable. I'm just wondering why your IDE isn't telling you that.

    Take this line:
    Code:
    this.getServer().getScheduler().scheduleAsyncDelayedTask(
                                                    this,
                                                    new Runnable(){
                                                            @Override
                                                            public void run(){
                                                                    rfplayer.sendMessage("Cleared " + Con.clearConnectedBlocks(rfplayer.getTargetBlock(null, 100)) + " blocks.");
                                                            }
                                                    }
                                    );
    It's the same as this:
    Code:
    public class AsyncTaskFoo implements Runnable
    {
      @Override
      public void run()
      {
        rfplayer.sendMessage(...);
      }
    }
    and doing this:
    Code:
    this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new AsyncTaskFoo());
    
    So, what do you guess rfplayer is inside AsyncTaskFoo? null, exactly.

    Edit: actually, it's not null but undeclared so it shouldn't even compile.
     
  7. Offline

    vildaberper

    Explain to me why I can send messages and get the target block from rfplayer if its null.

    Please, rfplayer is not null. That is not the problem.

    Edit: And not undeclared either.
     
  8. Offline

    MrChick

    Take a look at my edit. rfplayer IS null, at least at one point in your code.

    Edit: darn, again, it's not null but undeclared -.-

    Prove it to me that it isn't.

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

    vildaberper

    It works.
    Try it yourself, look at a floating island, then do /rf. Make sure youre OP.
    https://www.sugarsync.com/pf/D434924_7509600_253785

    Thats the end of that, it is not my problem.
    The problem is that i sometimes get NullPointerException when I compare a block material with air.
     
  10. Offline

    MrChick

    Alright, I just checked, seems like you actually can access variables of the parent class inside an anonymous inner class... sorry for that, I was wrong on that one.
    still, since you're doing an async task, the onCommand may already have finished at the point your task is run.

    You might want to try to create a class implementing runnable, passing your rfplayer to it and schedule that one as a task.
     
  11. Offline

    Kekec852

    Try putting some debug statments to find the what exactly is null. And try using sync call when modifing world.
     
  12. Offline

    vildaberper

    I dont see the difference?

    Good point, i didnt put any debug stuff in the code! xD

    Sync will lock up the whole server, so thats out of the question. "/
     
  13. Offline

    SpaceManiac

    Unfortunately, if the null pointer exception is happening somewhere random and only sometimes, it is 100% the fact that your timer is async. You CANNOT modify the world or any other API calls inside an async callback. You'll have to figure out some way to split the search into smaller tasks that can be run synchronously.
     
  14. Offline

    vildaberper

    Really?
    Thats most retarded. "/

    Oh well, thanks.

    Edit: Fixed.
    Heres how I did it:
    http://pastebin.com/5V0Vr9nL

    Works like a charm, many thanks SpaceManiac!
     
Thread Status:
Not open for further replies.

Share This Page