I am writing a program to mimic the "Deal or No Deal" game.
Background on Deal or No Deal: http://en.wikipedia.org/wiki/Deal_or_No_Deal
In working up to the final product, I have written various classes. However, I am trying to test one of my classes, and continue to get the NullPointerException.
I wrote a class called Box, that creates "box" objects. The box object is the actual box that a player picks. It consists of a true/false value and a double boxValue. The boolean variable denotes whether it's open/closed (true for open, false for closed). The double boxValue is the actual value assigned to the box.
public class Box {
//instance fields
private double boxValue; // the amount of $ in each box
private boolean openClose; // whether or not the box is closed
//Constructor
public Box(double boxValue) {
this.openClose = false;
this.boxValue = boxValue;
if (boxValue < 0) {
throw new IllegalArgumentException("Box value must be greater than 0");
}
}
}
I have written another class called BoxList as well. This creates a Box object array that will serve as the playing board, when combined with further classes that I am planning to write. The main idea is that the constructor in BoxList creates the array by using a passed in double array as a paramater, and then creates a Box Object array of the same length of the passed in double array, and assigns the double value of each element of the parameter array, to the value of the Box object array as the corresponding element.
I wrote a sample main method to test, but when I try to get the value of a particular element of a Box in the BoxList array, I get the NullPointerException. Can anyone offer advice to help trouble shoot this.
(These are snips of a larger program...I already wrote so much that I didn't want to clog further)
public class BoxList {
private Box[] boxArray;
public BoxList(double[] monetaryAmounts) {
Box[] boxArray = new Box[monetaryAmounts.length];
for (int i = 0; i < boxArray.length; i++) {
boxArray[i] = new Box(monetaryAmounts[i]);
}
}
public double getValue(int index) {
return boxArray[index].getValue();
}
// A sample main method to test out various object methods
public static void main(String[] args) {
double[] monetaryAmounts = {.5, 1, 3, 7.5, 8, 10}; // test array
BoxList test = new BoxList(monetaryAmounts);
System.out.println(test.getValue(0));
}
return boxArray[index].getValue();