0

I am writing this basic program with 3 classes in 3 separate files. It is supposed to be a program consisting of a class called PairOfDice, composed of two Die objects. Included methods to set and get the individual die values, a method to roll the dice,and a method that returns the current sum of the two die values. A driver class called RollingDice to instantiate and use a PairOfDice object.

It compiles without problems but when I try to run it results in a run-time error as follows:

Exception in thread "main" java.lang.NullPointerException at PairOfDice.rollDice(PairOfDice.java:42) at RollingDice.main(RollingDice2.java:16)

public class RollingDice
{
    public static void main(String[] args)
    {
        PairOfDice pairofdice=new PairOfDice();

        pairofdice.rollDice();

        System.out.println(pairofdice.getDie1() +"\t" +pairofdice.getDie2());
     }
}


public class PairOfDice
{
    private Die die1, die2;

    public PairOfDice()
    {
        Die die1=new Die();
        Die die2=new Die();
    }

    public int getDie1()
    {
        return die1.getFaceValue();
    }

    public int getDie2()
    {
        return die2.getFaceValue();
    }

    public void setDie1(int dieValue)
    {
        die1.setFaceValue(dieValue);
    }

    public void setDie2(int dieValue)
    {
        die2.setFaceValue(dieValue);
    }

    public void rollDice()
    {
        die1.roll();
        die2.roll();
    }

    public int sum()
    {
        return die1.getFaceValue()+die2.getFaceValue();
    }
}


public class Die
{
    private final int MAX = 6;
    private int faceValue;

    public Die()
    {
        faceValue = 1;
    }

    public void roll()
    {
        faceValue = (int)(Math.random() * MAX) + 1;
    }

    public void setFaceValue(int value)
    {
        faceValue = value;
    }

    public int getFaceValue()

    {
        return faceValue;
    }

    public String toString()
    {
        String result = Integer.toString(faceValue);
        return result;
    }
}
  • 1
    The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, here `PairOfDice.java:42`, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Jul 04 '17 at 16:40
  • Take a good long look at constructor for `PairOfDice`. – M. Prokhorov Jul 04 '17 at 16:41
  • You're shadowing the die1 and die2 variables. Don't do that. Don't re-declare them in the constructor. – Hovercraft Full Of Eels Jul 04 '17 at 16:41
  • Thank you. I corrected for the re-declaration and now it works fine. –  Jul 04 '17 at 16:53

1 Answers1

0

use

 public PairOfDice()
    {
        die1=new Die();
        die2=new Die();
    }

You are trying to declare die1 and die2 in constructor. Thus the local variables were not initializing.

stinepike
  • 54,068
  • 14
  • 92
  • 112