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

2011/12/31

Need Help - nullPointerException and making score board

thetibster thetibster

2011/12/31

#
I'm programming a version of Pong, and I have almost everything sorted out except the scoreboard. I'm not very good at doing scoreboards, so I compared a lot of different ones from different projects I have saved and tried to throw together one of my own. When I can get it to compile, it never displays the scoreboard properly, so I think I wrote it incorrectly, but my biggest problem right now is that it compiles with no syntax errors, but every time I try and add it to the world I get a nullPointerException, and I can't figure out why. Can anyne help me with this?? I'll post the source code for the ScoreBoard class below:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import java.awt.Color;
import java.lang.String;

/**
 * Write a description of class ScoreBoard here.
 * 
 * @author Emily Sullivan
 * @version 1.0
 */
public class ScoreBoard extends Actor
{
    private String text1 = "Player 1 : ";
    private String text2 = "Player 2 : ";
    private int score1;
    private int score2;
    private String player1score = String.valueOf(score1); 
    private String player2score = String.valueOf(score2); 
    // I have no clue if these last two variables are used correctly or if I need them or anything, I just tried it because I saw it somewhere and I needed to try something different, because what I was doing wasn't working.
     
    /**
     * Constructor for initial scoreboard.
     */
    public ScoreBoard()
    {
        player1score = "0";
        player2score = "0";
        updateImage();
    }
    
    /**
     * Constructor for ScoreBoard.
     * @param: Name "Player 1", player 1's score, "player 2", player 2's score
     */
    public ScoreBoard(String text1, String score1, String text2, String score2)
    {
        this.text1 = text1;
        this.text2 = text2;
        player1score = score1;
        player2score = score2;
        updateImage();
    }
    
    /**
     * Act - do whatever the ScoreBoard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        updateImage();
    }    
    
    /**
     * Update the scoreboard to show the current score.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(550, 40);
        Font font = new Font("Courier", Font.BOLD, 15);
        image.setColor(Color.YELLOW);
        image.setFont(font);
        image.drawString(text1 + player1score, 0, 0);
        image.drawString(text2 + player2score, getWorld().getWidth()/2 + 2, 0);
        setImage(image);
    }
}
danpost danpost

2011/12/31

#
There is an example of a scoreboard in 'Need help making a scoreboard', http://www.greenfoot.org/topics/674 Look it over and see how you could simplify things. Off hand, you can eliminate all the parameters for the scoreboard constructor and eliminate lines 38 through 41; you can eliminate lines 18 and 19 (replace 'player1score' in lines 63 and 64 with score1, and ditto for 2); with lines 16 and 17, set both values to zero, and eliminate lines 27 and 28; line 4 is not needed; you need a method with two parameters (player number and score change value) to adjust player scores; you could put 'updateImage();' as the last line in the score changing method above and eliminate it from act().
danpost danpost

2011/12/31

#
Also, with lines 63 and 64, the y-coordinate to draw the string at will be the low point of the string, so change them (the last parameter in those lines) to about 20. Oh, and one more thing, take 'getWorld()' out of the 'updateImage()' method. This not only will cause a run-time error when called from the constructor (because the scoreboard is not yet in the world), but it is probably not what you intended to be there. You most likely wanted 'image.getWidth() /2 + 2' instead.
thetibster thetibster

2011/12/31

#
Thank you! I'll go and try and fix some of that now, see how it works. thanks! :)
kiarocks kiarocks

2011/12/31

#
And your null pointer also came from that line
getWorld().getWidth()
because there was no world to get the width of.
thetibster thetibster

2011/12/31

#
YESS thank you thank you thank you all :) Got it! :)
danpost danpost

2011/12/31

#
GOD bless, and Happy New Year!
thetibster thetibster

2011/12/31

#
You too! :D
You need to login to post a reply.