-1

The program has a class relationship in which WeightedDie extends the Die class as follows:

import java.util.Random;

public class Die
{
    private int _maxNumSides;
    private Random _rand;
    // class level variable with visibility for sub classes
    protected int _faceValue;
    public final static int DEFAULT_SIDES = 6;

    // no param constructor for normal 6 sided die
    public Die()
    {
        _rand = new Random();
        _maxNumSides = DEFAULT_SIDES;
        this.roll();
    }

    // will randomly reset the number of dots showing on a side
    public int roll()
    {   
        _faceValue = _rand.nextInt(_maxNumSides) + 1;
        return _faceValue;
    }

    ...

}

and

public class WeightedDie extends Die
{
    private int randomNum;
    private int maxNumSides = 6;
    private double [] weights;
    private Random rand;
    private double sum = 0;
    private double weightRoll;
    private int cumulWeight;
    private double cumulWeightDouble;


public WeightedDie()
{
    super();
}

public WeightedDie(double[] w)
{
   ...
}

public int roll()
{
    cumulWeight = 0;
    cumulWeightDouble = 0;
    weightRoll = 0;
    rand = new Random();

    randomNum = rand.nextInt(100) + 1;

    for(int i = 0; i < 6; i++)
    {
        weightRoll = weights[i]*100;
        cumulWeightDouble = weightRoll + cumulWeightDouble;
        cumulWeight = (int) cumulWeightDouble;

        if(randomNum >= (cumulWeight - weightRoll) && randomNum <= (cumulWeight))
        {
            _faceValue = i;
            break;
        }
    }
    return _faceValue;
}
}

My problem seems to be that the WeightedDie constructor uses the Die constructor with super(). In the Die constructor this.roll() is called, and this is calling WeightedDie's roll() instead of Die's roll(), which gives a null pointer. I want this.roll() in Die to call Die's roll() method. How can this be fixed?

knueser
  • 108
  • 1
  • 12
  • TBH I would remove `roll` from the constructor -it shouldn't be there- and than after the Object is constructed then you can call `roll` – Scary Wombat Dec 02 '16 at 05:27
  • Also see http://stackoverflow.com/questions/6386343/how-to-call-a-super-method-ie-tostring-from-outside-a-derived-class – Scary Wombat Dec 02 '16 at 05:29
  • Yes, that makes sense. This code was provided to me by a professor, so I am not sure if I am to alter it. However, I put a check in the subclass's roll method to check for null, and if so to call super.roll(). I think this should fix things, thanks! – knueser Dec 02 '16 at 05:32

2 Answers2

-1

In the Die constructor this.roll() is called, and this is calling WeightedDie's roll() instead of Die's roll(),

That's Correct but not a problem.

which gives a null pointer.

Show the stack trace. probably this line has issue:

weightRoll = weights[i]*100;

because weights array is not initialized.

See this: How do I declare and initialize an array in Java?

Community
  • 1
  • 1
Azodious
  • 13,385
  • 1
  • 32
  • 68
  • Yes, I am aware that the array is not initialized. I am wondering if it is possible to call Die's roll method in the Die constructor instead of the WeightedDie's roll method. As ScaryWombat said above, the code should be changed to not call roll in the constructor. – knueser Dec 02 '16 at 05:38
  • You have two ways: 1. Create Die's object instead of WeightedDie. OR 2. rename `roll` in Die class. if you override, that's the defined brhaviour. – Azodious Dec 02 '16 at 05:41
-1

we have below options to resolve this.

  1. invoke by super.roll() method resolving null pointer exception.
  2. change roll() method names in super and sub class
  3. change the roll() method in super class to private
snofty
  • 70
  • 7