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

2017/3/11

java.lang.NullPointerException when trying to create counter

SBRMR SBRMR

2017/3/11

#
So I need to create Flappy Bird and I need a counter, however it will never show up and whenever i fly through a pipe and the score goes up I get an error. Java says the error is on line 31 of the counter class. Thank you to everyone who would like to help :) This is my Counter class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    public static int score=0;
    private int puntenteller=0;
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
        Gameover();     
        puntenteller=puntenteller+1;
        
        
        // Add your action code here.
    }    
    private void Gameover()
    {
        if(BottomPipe.Gescoord==false)
        {
            score=score+1;
            setImage(new GreenfootImage("Score:"+score, 30, Color.BLACK, null));
           
            
        }
    }
}
This is my Bottompipe class which counts the points:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BottomPipe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BottomPipe extends Pipe
{
    //public int xverwijder=getX();
    public static boolean Gescoord=true;
    
    /**
     * Act - do whatever the BottomPipe wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
 
    {    
        
        
        
        move(-hSpeed);
        if(getX()==101&&Sound.playSound==true)
        {
            Counter.score=Counter.score+1;
            
            Greenfoot.playSound("sbting.wav");
            Gescoord=false;
            
        }
      
        
        // Add your action code here.
    }    
}
This is the World Class:
import greenfoot.*;  // (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 FlappyWorld extends World
{
    private int timer=0;
    private static int pipedistance=120;


  
    public FlappyWorld()
    {    
     
        super(600, 400, 1,false); 
        prepare();
       

    }

    public void act()
    {
        timer();
        //timergeluid();

        gameovervoid();

    }

    private void prepare()
    {
        addObject(new FlappyBird(),100,200);
        //addObject(new GameOver(),300,200);
        addObject(new Sound(),577,379);
        addObject(new Counter(),60,20);

    }
    

    
    private void timer()
    {
        timer=timer+1;
        if (timer==100)
        {
            newpipes();
            //newpipeboven();
            timer=0;

        }
    }

    /*private void timergeluid()
    {
    timergeluid=timergeluid+1;
    if(timergeluid==120)
    {
    Greenfoot.playSound("sbting.wav");
    timergeluid=0;

    }
    } */

    
    private void newpipes()
    {
        BottomPipe onder=new BottomPipe();
        TopPipe boven=new TopPipe();
        int y = getHeight() - onder.getImage().getHeight()/2 + Greenfoot.getRandomNumber(onder.getImage().getHeight()-77);
        addObject(onder, getWidth()+ onder.getImage().getWidth()/2, y); 
        addObject(boven, getWidth()+onder.getImage().getWidth()/2,y-200-pipedistance);

    }

    public void gameovervoid()
    {

        if(FlappyBird.GAmeover==false)
        {
            //Greenfoot.stop();
            addObject(new GameOver(),300,200);
            FlappyBird.GAmeover=true;
            Greenfoot.stop();

        }
    } 
    /*private void newpipeboven()
    {
    /*TopPipe boven=new TopPipe();
    int y = (getHeight() - boven.getImage().getHeight() + Greenfoot.getRandomNumber(boven.getImage().getHeight()-50));
    addObject(boven, getWidth()+ boven.getImage().getWidth()/2, y); */

}
davmac davmac

2017/3/11

#
There is a bug in the current version of Greenfoot; you can't pass 'null' as the foreground or background color. This will be fixed in the next release. In the meantime, just pass a specific colour:
            setImage(new GreenfootImage("Score:"+score, 30, Color.BLACK, new Color(0,0,0,0)));
SBRMR SBRMR

2017/3/11

#
Thanks a lot! I'll give it a go as soon as i get home tonight.
You need to login to post a reply.