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); } }