0

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

GridMonitor Contructor

// 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
        }
    }

Get SurroundingSumGrid Method

public double[][] getSurroundingSumGrid() {
        // TODO Auto-generated method stub
        double[][] surroundingSumGrid = this.surroundingSumGrid;
        return surroundingSumGrid;
    }

Edit

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

Banani720
  • 159
  • 3
  • 17
  • 1
    You haven't made a copy, you just assigned the same reference to it. – Z. Kosanovic Sep 15 '20 at 20:48
  • How should I go about making a copy of it with correct encapsulation, I was under the impression that if you did something like `int b = 3` then `int a = b` then a will hold the value of 3? Would this not fall under the same concept – Banani720 Sep 15 '20 at 20:48
  • 1
    It does, but when you assign `a = b`, you're copying the `int` value of `3` into `a`. If you do the same with an object variable, you're copying the object reference, which points to the same object. (This is called _aliasing_.) – chrylis -cautiouslyoptimistic- Sep 15 '20 at 20:53
  • hmm ok how would i go about making a copy then as opposed to just aliasing it – Banani720 Sep 15 '20 at 20:55
  • https://stackoverflow.com/q/5617016/120955 – StriplingWarrior Sep 15 '20 at 20:57

1 Answers1

1

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.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • in terms of this program (only part is shown here) there are four different arrays that will be called 6 times each for different test cases should I be worried about the memory allocation in that regard? or do you think it should be ok, running on my local machine is fine but this assignment will be submitted to an onyx server so not sure of the impact there – Banani720 Sep 16 '20 at 01:52
  • Obviously it'll depend on a lot of variables like the sizes of your grids, but in general I wouldn't be worried if the method is only called a dozen times or less. The problems tend to arise when you're invoking the method hundreds, thousands, etc. of times, so I find it helpful to name the method in a way that will prevent other developers from calling the method without consideration for its cost. If this is a one-time assignment, there's less reason for you to worry about that aspect. – StriplingWarrior Sep 16 '20 at 16:36