0

I am getting the following error:

Exception in thread "main" java.lang.NullPointerException
    at BallContainerImage.update(BallContainerImage.java:101)
    at BallContainer.addBall(BallContainer.java:93)
    at Game.ejectBall(Game.java:92)
    at LotteryTestB.main(LotteryTestB.java:19)

Line 19 contains:

      dramaticGame1.ejectBall();

the Dramatic Game class contains the following:

public class DramaticMachine extends Machine
{
  // Constructor is given the person's name.
  public DramaticMachine(String name, int length)
  {
    super(name, length);
  }


  public Ball ejectBall()
  {          
    if (getNoOfBalls() >= 0)
      return null;
    else
    {
      //Math.random() * getNoOfBalls yields a number
      //which is >=0 and < number of balls.
      int ejectedBallIndex = (int) (Math.random() * getNoOfBalls());

      for (int selectedBallIndex = 0; selectedBallIndex < ejectedBallIndex; selectedBallIndex++) 
  {
    Ball selectedBall = getBall(selectedBallIndex);
    selectedBall.flash(4, 5);
  }

  Ball ejectedBall = getBall(ejectedBallIndex);
  ejectedBall.flash(4, 5);

  swapBalls(ejectedBallIndex, getNoOfBalls() -1);
  removeBall();

  return ejectedBall;


}//else
  }//ejectBall


  public String getType()
  {
    return "Dramatic Lottery Machine";
  }//getType
}//dramaticMachine

How can i fix this?

This is the code for the DramaticGame class:

public class DramaticGame extends Game
{
  // Constructor is given the person's name.
  public DramaticGame(String machineName, int machineSize, String rackName, int
  rackSize)
  {
    super(machineName,machineSize,rackName,rackSize);

  }

  public Machine makeMachine(String machineName, int machineSize)
  {
    return new DramaticMachine(machineName, machineSize);
  }//makeMachine
}

This is the code for LotteryTestB:

public class LotteryTestB
{
  public static void main (String args[])
  {
    SpeedController speedController
      = new SpeedController(SpeedController.HALF_SPEED);
    LotteryGUI gui = new LotteryGUI("TV Studio", speedController);

    Worker worker = new TraineeWorker("Jim",0);

    DramaticGame dramaticGame1 = new DramaticGame("Lott O'Luck Larry", 49,
                                              "Slippery's Mile", 7);
    gui.addGame(dramaticGame1);

    worker.fillMachine(dramaticGame1);

    for (int count = 1; count <=dramaticGame1.getRackSize(); count++)
    {
      dramaticGame1.ejectBall();
      speedController.delay(40);
    }//for

}//main
}//LotteryTestB
12
  • please show a complete stack trace, and probably more code will also be needed [what is getNoOfBalls()? what is getBall()? ...] Commented Feb 19, 2012 at 14:35
  • 2
    it seems like you dramaticGame1 variable is null, please provide the code where this variable is created. Commented Feb 19, 2012 at 14:37
  • getNoOfBalls() and getBall() are stored in classes in a jar file, which cannot be edited. They are from the superclass Machine. Commented Feb 19, 2012 at 14:42
  • @user1148677: and what is the complete stack trace? the two lines you attached? or is there more? Commented Feb 19, 2012 at 14:43
  • Does gui.addGame(dramaticGame1) or worker.fillMachine(dramaticGame1) modify the DramaticGame object? Are there any caused by sections in the stack trace? Commented Feb 19, 2012 at 14:45

1 Answer 1

2

NullPointerException is one of the easier problems to chase down. It means that some reference wasn't initialized properly. It should be easy to figure out by stepping through your code with a debugger.

If you are incapable of using a debugger, the stack trace makes this easy for you. There are only four places to look, and it says exactly where they are.

at BallContainerImage.update(BallContainerImage.java:101)
at BallContainer.addBall(BallContainer.java:93)
at Game.ejectBall(Game.java:92)
at LotteryTestB.main(LotteryTestB.java:19)

It's not the bottom one. The reference to dramaticGame is the only one on that line, and you call new to initialize it. Go on to the next one. Add a log or print statement to prove where the null reference is, then go and initialize it properly.

I don't think your code is layered properly. You'll never get this working unless you can decompose the problem into smaller chunks, unit test them until they work, and then use that code to build up the complex solution.

Separate UI from the game itself. Get the game working, then worry about display issues.

Sign up to request clarification or add additional context in comments.

5 Comments

+1: I surprises me how often people don't use the debugger to debug their code when its usually the button next to Run in their ide. ;)
Get IntelliJ community edition and this problem will be sorted out in five minutes.
I prefer IntelliJ CE too, but netbeans, eclipse and many others also let you debug.
They do, but I hate recommending inferior products.
That's not what makes it inferior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.