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

2018/1/5

java.lang.NullPointerException

L2plex L2plex

2018/1/5

#
Hello I would like to create a Labyrinth. At the moment I'm working on the walls so my actor can't pass them. I createt a matrix for the playground in the World-class and I need access to this class in the actor-class. I tried at least 3 hours to find a solution, but unfortunately I don't get it. : ( 1.) part-code of the World-class
public boolean solid(int a, int b)
    {
       int x = a;
       int y = b;
             
       int ID = matrix [x][y];
              
       if(ID == 1){
          return false;
       }
       else{ 
          return true;       
        }
    
    }
2.) code of the actor-class:
  /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        collisionCheck();

    } 
    
    /**
     * Test for collision
     */
    public void collisionCheck()
    {
       int a = 0;
       int b = 0;
       
       a = this.getX()+1% blockgrösse;
       b = this.getY()+1% blockgrösse; 
       
       myWorld.solid(a,b);
        
        
        
        if (moveRight == true)
        {

            if (m_World.solid(a,b)== true)
            {
                move();
                
            }
                         
        }
                            
         if (moveLeft == true)
        {
            
           if (m_World.solid(a,b)== true)
            {
                move();
                
            }

        }
       
        if (moveUp == true)
        {    
                                   
            if (m_World.solid(a,b)== true)
            {
                move();
                
            }

        }
            
        if (moveDown == true)
        {
           if (m_World.solid(a,b)== true)
            {
                move();
                
            }

        }
  
    }
        
    public void move(){
        moveLeft = false;
        moveRight = false;
        moveUp = false;
        moveDown = false;  
        
        if (Greenfoot.isKeyDown("a")){
            setLocation(getX()-speed,getY());
            moveLeft = true;

        } 
        if (Greenfoot.isKeyDown("d")){
            setLocation(getX()+speed,getY());
            moveRight = true;

        } 
        if (Greenfoot.isKeyDown("w")){
            setLocation(getX(),getY()-speed);
            moveUp = true;
            
        }
        if (Greenfoot.isKeyDown("s")){
            setLocation(getX(),getY()+speed);
            moveDown = true;
            
        }
     
    }
Super_Hippo Super_Hippo

2018/1/5

#
What is 'myWorld' and what is 'm_World'? If the name of your world class is 'MyWorld', then that's what you have to do:
if (((MyWorld) getWorld()).isSolid())
{
    //...
}
You can change the 'solid' method (I named it 'isSolid' because it asks if it is solid) to the following (it's doing the same):
public boolean isSolid(int a, int b)
{
    return matrix[a][b] != 1;
}
L2plex L2plex

2018/1/5

#
the name of my World is Labyrinth, I changed it now to:
if (((Labyrinth) getWorld()).solid(a,b))
            {
                move();
                
            }
L2plex L2plex

2018/1/5

#
but the problem is still the same... ^^'
L2plex L2plex

2018/1/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Creature
{
    int speed = 5;
    private int blockgrösse = 30;
    
    public boolean moveLeft = false;
    public boolean moveRight = false;
    public boolean moveUp = true;
    public boolean moveDown = false;
    
   Labyrinth myWorld = (Labyrinth) getWorld();
 
    public void act() 
    {
        collisionCheck();

    } 
    
    /**
     * Test for collision
     */
    public void collisionCheck()
    {
       int a = 0;
       int b = 0;
       
       a = this.getX()+1% blockgrösse;
       b = this.getY()+1% blockgrösse; 
       
       myWorld.isSolid(a,b);
        
        
        
       if (moveRight == true)
       {

            if (((Labyrinth) getWorld()).isSolid(a,b))
            {
                move();
                
            }
                         
       }
                            
       if (moveLeft == true)
       {
            
           if (((Labyrinth) getWorld()).isSolid(a,b))
            {
                move();
                
            }

       }
       
       if (moveUp == true)
       {    
                                   
           if (((Labyrinth) getWorld()).isSolid(a,b))
           {
                move();
                
           }

       }
            
        if (moveDown == true)
        {
           if (((Labyrinth) getWorld()).isSolid(a,b))
           {
                move();
                
           }

        }
  
    }
        
    public void move(){
        moveLeft = false;
        moveRight = false;
        moveUp = false;
        moveDown = false;  
        
        if (Greenfoot.isKeyDown("a")){
            setLocation(getX()-speed,getY());
            moveLeft = true;

        } 
        if (Greenfoot.isKeyDown("d")){
            setLocation(getX()+speed,getY());
            moveRight = true;

        } 
        if (Greenfoot.isKeyDown("w")){
            setLocation(getX(),getY()-speed);
            moveUp = true;
            
        }
        if (Greenfoot.isKeyDown("s")){
            setLocation(getX(),getY()+speed);
            moveDown = true;
            
        }
     
    }
    
    
}
L2plex L2plex

2018/1/5

#
they say the error is because of the line 38...
danpost danpost

2018/1/5

#
L2plex wrote...
they say the error is because of the line 38...
Line 19 has 'myWorld' set to null because the line is executed at the time the playrer is created (beforre it could possibly be placed into a worrld). Remove line 19 and change 'myWorld' in line 38 to '((Labyrinth)getWorld())'.
Super_Hippo Super_Hippo

2018/1/5

#
Simply remove line 38, it is not doing anything anyway.
L2plex L2plex

2018/1/6

#
Thanks danpost. Now it works... ; )
You need to login to post a reply.