空指针异常在复制实例变量后出现?

huangapple go评论72阅读模式
英文:

Null Pointer Exception after making copy of instance variable?

问题

我有一个GridMonitorClass,它从文件中读取数据以获取basegrid,然后我们需要从中获取一个surrounding sum grid、average grid等。我设置了一个算法来获取surrounding sum grid,然后在我创建的get函数中(我相信)复制了那个grid,然后将方法设置为返回它。然而,当测试场景尝试运行该函数时,它会抛出一个NullPointerException,我不确定为什么... 下面是我的GridMonitor构造函数和getSurroundingSumGrid方法:

GridMonitor 构造函数

// 实例变量
private double[][] baseGrid;
private double[][] surroundingSumGrid;
private double[][] surroundingAvgGrid;
private double[][] deltaGrid;
private boolean[][] dangerGrid;

// 构造函数
public GridMonitor(String fileName) {

		try {
			this.baseGrid = readFile(fileName);
			// 获取其余网格的网格尺寸
			int baseGridXDimension = this.baseGrid.length;
			int baseGridYDimension = this.baseGrid[0].length;

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

获取 SurroundingSumGrid 方法

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

编辑

我尝试仅返回this.surroundingSumGrid,但仍然遇到空指针异常,因此我对为什么会发生这个错误感到困惑。

英文:

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 &lt; baseGridXDimension; i++) {
				for (int j = 0; j &lt; baseGridYDimension; j++) {
					if (i == 0 &amp;&amp; j == 0) {
						this.surroundingSumGrid[0][0] = (2 * this.baseGrid[0][0]) + this.baseGrid[0][1]
								+ this.baseGrid[1][0];
					} else if (i == 0 &amp;&amp; j != 0 &amp;&amp; j &lt; 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 &amp;&amp; j != 0 &amp;&amp; i &lt; baseGridXDimension - 1 &amp;&amp; j &lt; 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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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

答案1

得分: 1

在Java中,变量通过引用来引用数组(而通过值来引用原始数据类型)。这意味着当您进行此赋值时:

double[][] surroundingSumGrid = this.surroundingSumGrid;

您只是在说surroundingSumGrid应该指向与this.surroundingSumGrid相同的内存位置。要复制整个数组,您需要实例化一个新数组:

double[][] surroundingSumGrid = new double[baseGridXDimension][baseGridYDimension];

然后,您需要将旧数组的内容复制到新数组中。幸运的是,由于double是一个原始数据类型,您不必担心在数组内克隆单个对象。

不过要小心,因为如果每次调用getter时都创建一个全新的数组,您可能会分配大量未使用的内存。考虑将方法重命名,以使其更清楚地表明调用此方法将导致比仅获取字段值更多的工作发生。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月16日 04:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63909648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定