So here is the deal, I have been working on a coding project and I have a tester and a class-well 2 of each actually-and on my second class I keep receiving a null pointer value error. I believe it to be coming from my Pond constructor.
The class with actual problems is:
import java.util.Scanner;
/**
* This class represents a grid of lily pads in a pond
* for the purpose of playing Frogs and Toads.
*
* do not begin working on this until the Pad class is fully tested and debugged
* @author (your name)
* @version (a version number or a date)
*/
public class Pond
{
private Pad [][] grid; // the 2D array of Pad objects that make up our pond
int size; // the number of Pads in a row or column
/**
* Creates a grid of lily pads with height and width both equal
* to the given size; each lily pad is constructed with the appropriate
* status (frog, toad, or a vacant spot) for starting a game of
* Frogs and Toads.
*
* The starting configuration consists of a vacant
* lily pad in the center and the remaining lily pads being half
* occupied by frogs and half occupied by toads. Each (vertical)
* column with a higher index than the middle column starts with
* all toads, and each column with an index lower than the middle
* column starts with all frogs. The center column has frogs in
* the high row indices, toads in the low row indices, and a vacant
* spot in the middle.
*
* Internally, an instance of this class contains "size * size"
* instances of the Pad class (stored in a 2D array). The legal
* range of size is odd numbers from 3 up to 9, which is assumed
* by the constructor as a precondition.
*
* @param size - the height and width of the grid (assumed to be
* an odd number at least 3 and at most 9 as a precondition)
*/
public Pond(int size)
{
// check that size is an odd number between 3 and 9
// HINT size%2 computes the remainder of size/2.
// if (size%2 == 1) that means size is odd
// so there are three conditions on size: must be >=3, <=9 AND odd
grid = new Pad[size][size];
Pad a = new Pad(Pad.Frog);
Pad b = new Pad(Pad.Toad);
Pad c = new Pad(Pad.Vacant);
int d = (size-1)/2;
grid[d][d] = c;
int e,rows1;
int f=size-1;
for (int i=0; i<size; i++) {
for(int j=0; j<d; j++){
grid[i][j]=a;
}
for(int j=f; j>d; j--){
grid[i][j]=b;
}
}
// now fill each cell with a new Pad(Pad.XXXX)
// XXX will be FROG, TOAD, or VACANT
if (size%2 == 1 && size>=3 && size<=9){
this.size=size;
}
else {
System.out.println("Error size is not an odd number between 3 and 9");
System.exit(0);
}
// if this is NOT true, print an error message and return. Otherwise...
// declare the 2D array of Pad
}
/**
* Indicates whether or not all lily pads in the pond are in the solved state.
*
* @return true if the puzzle is solved and false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// create an int variable to count numSolved and set it to 0
int numSolved = 0;
// use a nested for loop to visit each cell in grid.
// If the cell at row, col is solved, then add one to numSolved
for (int i=0 ; i>size ; i++){
if (isSolved()==true){
numSolved = numSolved+1;
}
}
if (numSolved == size*size){
return true;
}
else{return false;}
// after the loop, if numSolved == size*size then it's solved (return true)
// otherwise it's not (false)
}
/**
* Returns a reference to the Pad object at the specified row and column of the pond.
* (note: rows and columns are counted starting from 0).
*/
public Pad getPad(int row, int col) {
// if row & col are between 0 and size-1,
if(-1>row && row<0 && -1>col && col<0){
return grid[row][col];
}
else {
Pad x = new Pad(Pad.Invalid);
return x;
// return grid[row][col]
// else return a reference to an INVALID Pad
}
}
/**
* Returns the width of the pond (which is the same as its height).
*/
public int getSize(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Please input size of grid");
size = keyboard.nextInt();
return size;
}
/**
* Resets the pond to the state it was in when originally created.
*/
public void reset(){
for (int i=0; i>size; i++){
for (int j=0; j>size; j++) {
Pad x = new Pad(Pad.Frog);
grid [i][j]=x;
reset();
}
}
}
/**
* Returns the current state of the pond as a String (with the VACANT, FROG,
* TOAD, and VACANT Pads represented as defined by the toString() method of
* the Pad class).
*/
public String toString(){
String result="";
for (int row=0; row<size; row++){
for (int col=0; col<size; col++){
result = result + grid[row][col].toString(); // add the next grid Pad in a row
}
result = result + "\n"; // go to a new line after each row
}
return result;
}
}
My tester class that is not showing error when run is:
/**
* Write a description of class PondTester2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PondTester {
public static void main(String [] args){
Pond pool = new Pond(3); // smallest size pool
assert(pool.toString().equals("FTT\nF T\nFFT\n")): "Pond toString failed.";
assert (pool.isSolved()==false): "Fail. Pond isSolved() should be false";
// 1) before starting you must fully complete and debug the Pad class first using PadTester
// 2) Then in Pond class define the isSolved, getPad, getSize and reset methods, then test here
// 3) activate the following test code and verify
// TEST CASE
assert pool.getSize()==3 : "Fail. Pond getSize() should return 3. Actual value returned "+pool.getSize() ;
assert pool.getPad(0,0).getStatus() == Pad.Frog : "Pond getPad(0,0) should return FROG";
assert pool.getPad(2,2).getStatus() == Pad.Toad : "Pond getPad(2,2) should return TOAD";
assert pool.getPad(1,1).getStatus() == Pad.Vacant : "Pond getPad(1,1) should return VACANT";
// Let's solve the 3x3 case
pool.getPad(0,0).setStatus(Pad.Toad);
pool.getPad(1,0).setStatus(Pad.Toad);
pool.getPad(2,0).setStatus(Pad.Toad);
pool.getPad(0,1).setStatus(Pad.Frog);
pool.getPad(2,1).setStatus(Pad.Toad);
pool.getPad(0,2).setStatus(Pad.Frog);
pool.getPad(1,2).setStatus(Pad.Frog);
pool.getPad(2,2).setStatus(Pad.Frog);
assert(pool.toString().equals("TFF\nT F\nTTF\n")): "Pond toString failed.";
assert pool.isSolved()==true : "Fail. Pond isSolved() should be true.";
pool.reset(); // back to start state
assert(pool.toString().equals("FTT\nF T\nFFT\n")): "Pond toString failed.";
assert pool.isSolved()==false : "Fail. Pond reset() did not reset pond.";
System.out.println("PondTester2 done. All tests passed.");
}
}
Pad is an object I made in another class I am just curious what is wrong with my constructor because it is not adding values, or so I am assuming.