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

2015/3/9

nullPointerException need help

Krhanos Krhanos

2015/3/9

#
I am a little new to greenfoot and just started using greenfoot and i get an error in this code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Ball extends Actor
{
        //sets Speed for the ball
        private int hSpeed = 3, vSpeed = 3;
    
    public void act()
    {
       movement();
       trail();
       Bounce();
    }
   
    
    public void Bounce() 
    { 
       //Makes the ball bounce off the bricks and paddle
       if(getWorld()!=null)
       {
           
       Actor paddle = getOneIntersectingObject(Paddle.class);
       
       if(paddle != null) 
       {
           vSpeed = -vSpeed;
           
        } 
        
        Actor Brick = getOneIntersectingObject(Bricks.class);
        
        if( Brick != null)
        {
            vSpeed = -vSpeed;
            getWorld().removeObject(Brick);
            
            Background world = (Background) getWorld();
            world.updateScore(); //<-------------------- Target code of error
        
        
        }
        
        
        if(getY()>getWorld().getHeight()-5)
        {
          
            getWorld().removeObject(this); 
       
           
        }
        
     } 
    
}


    public void movement()
    {
        //Sets speed and movement for the ball
         setLocation( getX() + hSpeed, getY() +vSpeed);
         
         if(getWorld()!=null)
         {
             
        if(getX()<5) {
            hSpeed = -hSpeed;
            
        }
        
        if(getY()<5) 
        {
            vSpeed = -vSpeed;
        }
        
        if(getX()>getWorld().getWidth()-5) 
        {
            hSpeed = -hSpeed;
        }
        
        if(getY()>getWorld().getHeight()-5) 
        {
           vSpeed = -vSpeed;
        }
        
        
        
         } 
         
    }
    
    public void trail()
    {
        //Creates the trail
        getWorld().addObject(new Trail(),getX(),getY());
    }
    
}
And this code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Background extends World
{
    Score score = new Score();
    private Score sb;
    /**
     * Constructor for objects of class Blackground.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 800, 1); 
        addObject(new Paddle(),getWidth()/2,700);
        addObject(new Ball(), getWidth()/2, getHeight()/2);
        addObject(new Score(), 55, 36);
        addBricksA(15);
        addBricksB(15);
        addBricksC(15);
        addBricksD(15);
    }
    
    public void updateScore()
    {
        sb.updatePoints();
    }
    
    public void addBricksA(int n)
    {
        for(int b=0; b<n; b++)
        {  
        addObject(new Bricks(),30+ 39*b,65);
    }
    }
    
    public void addBricksB(int n)
    {
        for(int b=0; b<n; b++)
        {  
        addObject(new Bricks(),30+ 39*b,100);
    }
    }
    
    public void addBricksC(int n)
    {
        for(int b=0; b<n; b++)
        {  
        addObject(new Bricks(),30+ 39*b,135);
    }
    }
    
    public void addBricksD(int n)
    {
        for(int b=0; b<n; b++)
        {  
        addObject(new Bricks(),30+ 39*b,170);
    }
   
    }
    
    public void GameOver()
    {
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Bricks.class));
        removeObjects(getObjects(Paddle.class));
        removeObjects(getObjects(Trail.class));
        
    }
    
    public void GameWon()
    {
        removeObjects(getObjects(Ball.class));
        removeObjects(getObjects(Bricks.class));
        removeObjects(getObjects(Paddle.class));
        removeObjects(getObjects(Trail.class));
        
    }
}
Any help would be awesome
Krhanos Krhanos

2015/3/9

#
should also say the error only happens when i hit a brick in my breakout code
danpost danpost

2015/3/9

#
Your error does not appear occurring at the line you indicated. Your error message probably has a line above the one you saw that has this in it: at Background.updateScore(Background.java:27) I do not see where 'sb' is assigned any value in the Background class (its value is still 'null' when trying to call a method on it). You can change line 18 of the Background class to the following to fix it:
addObject(sb = new Score(), 55, 36);
Krhanos Krhanos

2015/3/9

#
Oh my god. Thank you so much it worked!
You need to login to post a reply.