AkshaiShah
AkshaiShah

Reputation: 5939

exception in thread "main" Java.lang.nullPointerException error?

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

Upvotes: 0

Views: 3638

Answers (1)

duffymo
duffymo

Reputation: 308998

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.

Upvotes: 2

Related Questions