I have a GridMonitorClass that reads in a file to get a basegrid and then from there we have to get a surrounding sum grid, average grid etc, I set up an algorithm to get the surrounding sum grid then in the get function I made (I believe) a copy of that grid and then set the method to return it. However, when the test scenario tries to run the function it spits back a NullPointerException and I'm not sure why... below is my GridMonitor constructor and the getSurroundingSumGrid method as well
// Instance Variables
private double[][] baseGrid;
private double[][] surroundingSumGrid;
private double[][] surroundingAvgGrid;
private double[][] deltaGrid;
private boolean[][] dangerGrid;
//Constructor
public GridMonitor(String fileName) {
try {
this.baseGrid = readFile(fileName);
// Get Grid Dimensions for the Remaining Grids
int baseGridXDimension = this.baseGrid.length;
int baseGridYDimension = this.baseGrid[0].length;
// Set Up Surrounding Sum Grid
this.surroundingSumGrid = new double[baseGridXDimension][baseGridYDimension];
for (int i = 0; i < baseGridXDimension; i++) {
for (int j = 0; j < baseGridYDimension; j++) {
if (i == 0 && j == 0) {
this.surroundingSumGrid[0][0] = (2 * this.baseGrid[0][0]) + this.baseGrid[0][1]
+ this.baseGrid[1][0];
} else if (i == 0 && j != 0 && j < baseGridYDimension - 1) {
this.surroundingSumGrid[0][j] = this.baseGrid[0][j] + this.baseGrid[1][j]
+ this.baseGrid[0][j - 1] + this.baseGrid[0][j + 1];
} else if (i != 0 && j != 0 && i < baseGridXDimension - 1 && j < baseGridYDimension - 1) {
this.surroundingSumGrid[i][j] = this.baseGrid[i - 1][j] + this.baseGrid[i + 1][j]
+ this.baseGrid[i][j + 1] + this.baseGrid[i][j - 1];
} else if (i == 0 && j == baseGridYDimension - 1) {
this.surroundingSumGrid[0][j] = (2 * this.baseGrid[0][j]) + this.baseGrid[1][j]
+ this.baseGrid[0][j - 1];
} else if (i == baseGridXDimension - 1 && j == 0) {
this.surroundingSumGrid[i][0] = (2 * this.baseGrid[i][0]) + this.baseGrid[i][1]
+ this.baseGrid[i - 1][0];
} else if (i == baseGridXDimension - 1 && j == baseGridYDimension - 1) {
this.surroundingSumGrid[i][j] = (2 * this.baseGrid[i][j]) + this.baseGrid[i - 1][j]
+ this.baseGrid[i][j - 1];
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
}
public double[][] getSurroundingSumGrid() {
// TODO Auto-generated method stub
double[][] surroundingSumGrid = this.surroundingSumGrid;
return surroundingSumGrid;
}
I tried just returning this.surroundingSumGrid
and am still faced with a null-pointer exception so I am effectively lost in why this error is happening
In Java, variables refer to arrays by reference (whereas they refer to primitive data types by value). That means that when you make this assignment:
double[][] surroundingSumGrid = this.surroundingSumGrid;
You are just saying surroundingSumGrid
should point to the same place in memory as this.surroundingSumGrid
. To make a copy of the entire array, you'll need to instantiate a new array:
double[][] surroundingSumGrid = new double[baseGridXDimension][baseGridYDimension];
And then copy the array contents from the old array into the new one . Fortunately, since double
is a primitive data type, you don't have to worry about cloning individual objects within the array.
Be a little careful, though, because if you're creating a whole new array every time the getter is called, you may end up allocating a lot of memory that you never use. Consider renaming the method to make it clearer that calling this method will cause more work to happen than simply getting a field's value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.