This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kraken
Kraken wrote ...

2016/4/8

Null Pointer Exception when accessing the world class from an actor subclass

Kraken Kraken

2016/4/8

#
I don't know what is going on, would someone help, please? I keep getting the null pointer exception when i access a world class variable from an actor subclass. This is the code in question
       if(Greenfoot.isKeyDown("J")){
           getWorld().removeObjects(getWorld().getObjects(Instructions.class));
           myWorld.Level = 2;
           
        }
and this is what it's referencing
import greenfoot.*;
import java.util.*;
import java.util.List;   // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
        
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public int State = 1;
    public int Level = 0;
   public Player Player= new Player();
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        increaseLevel();
        
            
        
        
    }
    
    public void increaseLevel(){
       
        Level++; 
        if(Level ==1){
            addObject(new Instructions(), 400, 300);
            
        }
        if(Level ==2){
                        //Walls
            

        addObject(new WallDown(), 150, 100);
        addObject(new WallDown(), 150, 500);
        addObject(new Wall(), 750, 225);
        addObject(new Wall(), 750, 375);
        
        //Player
        
        Player.setLocation(50, 300);
        
        //Guards
        
        addObject(new Guard(), 375, 200);
        addObject(new Guard(), 500, 450);
        
        //Button and wires
        addObject(new Button(), 50, 200);

        
        addObject(new Gate(), 650, 300);
        //bounce block
        addObject(new Block2(), 375, 50);
        addObject(new Block(), 375, 550);
        addObject(new Block2(), 500, 50);
        addObject(new Block(), 500, 550);
        
        addObject(new Exit(), 795, 300);
        Player.speed =5;
        }

        if(Level ==3){
           
                        //Walls
            
           
            addObject(new Wall(), 450, 213);
            addObject(new Wall(), 550, 213);
            addObject(new Wall(), 100, 213);
            
            addObject(new WallDown(), 186, 300);
            addObject(new WallDown(), 350, 300);
            addObject(new WallDown(), 650, 100);
            addObject(new WallDown(), 650, 300);
            
            //Player
          
            
            //End
            addObject(new Exit(), 750, 0);
            
            //Gate
            addObject(new gateS(), 730, 275);
           

            //Player
            Player.setLocation(100, 300);
            addObject(new Button(), 300, 100);
            
            addObject(new GuardS(), 300, 100);
            addObject(new Guard(), 480, 400);
            
            addObject(new Block2(), 100, 100);
            addObject(new Block(), 600, 100);
            
            addObject(new Block2(), 480, 280);
           
            
            addObject(new Block(), 480, 560);
            Player.speed=6;
            
            
            
            
            
         
            
           
            
            
            
        }
        if(Level ==4){
            
            addObject(new Player(), 100, 400);
            
            
            
            addObject(new Block2(), 200, 50);
            addObject(new Block2(), 300, 50);
            addObject(new Block2(), 400, 50);
            addObject(new Block2(), 500, 50);
            addObject(new Block2(), 600, 50);
            addObject(new Block2(), 700, 50);
            
            
            addObject(new Block(), 200, 750);
            addObject(new Block(), 300, 750);
            addObject(new Block(), 400, 750);
            addObject(new Block(), 500, 750);
            addObject(new Block(), 600, 750);
            addObject(new Block(), 700, 750);
            
            
            
            
            addObject(new Guard(), 700, 300);
            addObject(new Guard(), 600, 400);
            addObject(new Guard(), 500, 200);
            addObject(new Guard(), 400, 100);
            addObject(new Guard(), 300, 450);
            addObject(new Guard(), 200, 500);
           
            
            addObject(new Exit(), 800, 400);
            Player.speed=8;
            

            

            
            
            
            
            
            
            
        }
        if(Level == 5){
            
            addObject(new Victory(), 400, 300);
           
  
        }
       
    }
    public void clear(){
        removeObjects(getObjects(Wall.class));
        removeObjects(getObjects(WallDown.class));
        removeObjects(getObjects(Block.class));
        removeObjects(getObjects(Block2.class));
        removeObjects(getObjects(Gate.class));
        removeObjects(getObjects(gateS.class));
        removeObjects(getObjects(Button.class));
        removeObjects(getObjects(Exit.class));
        
        
    }


    
   
}
danpost danpost

2016/4/8

#
Which line is being highlighted on the error? If it is line 2, then your actor may have been removed from the world before executing the line. If line 3, then 'myWorld' either was not assigned anything or it was assigned a 'null' value.
You need to login to post a reply.