【发布时间】:2026-01-21 06:00:02
【问题描述】:
我从一个完全正常的 2D 数组矩阵程序开始。我将其中的大部分转换为实现同一事物的链表版本。目前(使用 .printStackTrace)我发现我的空指针异常来自这一行 -> matrix.setValue(matrixRow, matrixColumn, value);
调用 setValue 并在此引发错误 -> a[x][y] = value; //这一行出错
我希望包含足够的代码,如果更多信息有帮助,请告诉我。暂时需要编码中断。非常感谢。
else { //matrix creation
matrix = new Node();
int matrixRow = 1;
int matrixColumn = 1;
for (int i = 1; i <= matrixSize; i++) {
//scanner one line at a time
scanner = new Scanner(fileIO.getNextLine());
if (scanner.hasNextDouble()) {
while (scanner.hasNextDouble()) {
value = scanner.nextDouble();
matrix.construct(matrixRow, matrixColumn);
matrix.setValue(matrixRow, matrixColumn, value);
matrixColumn++;
}//while
matrixRow++;
matrixColumn = 1;
}//if
}//for
if (matrix.isSquare()) {
determinant = matrixMaths.determinate(matrix);
fileIO.writeToOutput(matrix.toString());
fileIO.writeToOutput("The determinant is: " + String.valueOf(determinant));
fileIO.writeToOutput("\n\n");
} else {
errorMsg = "\n\nMatrix is not square.\n\n";
fileIO.writeToOutput(errorMsg);
}//else
}//else matrix creation
Node construct(int n, int m) {
Node[][] a = new Node[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Node ptr = new Node();
ptr.data = a[i][j];
ptr.right = null;
ptr.down = null;
a[i][j] = ptr;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != n - 1)
a[i][j].right = a[i][j + 1];
if (i != n - 1)
a[i][j].down = a[i + 1][j];
}
}
return a[0][0];
}
public void setValue(int x, int y, double value) {
a[x][y] = value; //error in this line
if (x > sizeX) {
sizeX = x;
}
if (y > sizeY) {
sizeY = y;
}
}
* 编辑 * 在调用 setValue 之前,我已经输入了 system.out 语句,以查看我正在将 1 的值传递给数组。在我得到错误之前,该方法只会走这么远。 所以,setValue(matrixRow, matrixColumn, value) -> setValue(1,1,value);首先通过while循环。我改成了这个
int matrixRow = 0;
int matrixColumn = 0;
然后在这里得到新的错误(构造方法中的最后一个返回):
return a[0][0];
另一个数组OutOfBounds... 我将尝试再次遵循逻辑,但不确定在哪里进行修复。请帮忙! 并且更改 row/column = 2 也会再次获得 NUllPtr。
所以,进行了一些更改,并且正在走得更远......
} else { //matrix creation
matrix = new Node();
int matrixRow = 0;
int matrixColumn = 0;
System.out.println("\n" + "matrixSize " + matrixSize);
for (int i = 1; i <= matrixSize; i++) {
//scanner one line at a time
scanner = new Scanner(fileIO.getNextLine());
if (scanner.hasNextDouble()) {
while (scanner.hasNextDouble()) {
value = scanner.nextDouble();
System.out.println(matrixRow + " row number");
System.out.println(matrixColumn + " column number");
System.out.println(value + " value");
matrix.construct(matrixRow, matrixColumn, value);
matrixColumn++;
}//while
matrixRow++;
matrixColumn = 1;
}
我的意见是这样的......
3
3 -2 4
-1 5 2
-3 6 4
输出通过矩阵大小 1 和 2。它到达第三个矩阵第 3 行第 0 列。在第 3 行第 0 列,我再次收到 ArrayIndexOutOfBounds 错误。
我让它工作了,但在设置值时遇到了麻烦,但是....该项目将是一个“矩阵作为链表”,这与之前的分配“矩阵作为二维数组”不同。所以也许我的结构一开始就不正确?此外,My Node 类是独立的(作为实现接口的 ADT)。有一个完整的“另一个类来寻找行列式”。目前不担心这个。此外,我们在输入中矩阵数据之前的行中给出了“矩阵大小”。再次感谢您!
**无法入睡编辑... 此构造适用于整个输入文件。所以现在我只需要弄清楚在哪里设置值。 (这里没有分配值,所以我可能会删除它,除非我能找到解决办法。
void construct(int n, int m, double value) {
System.out.println("construct " + n + " n " + m + " m " + value + " value");
Node[][] a = new Node[n + 1][m + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Node ptr = new Node();
ptr.data = a[i][j];
ptr.right = null;
ptr.down = null;
a[i][j] = ptr;
a[i][j].right = a[i][j + 1];
a[i][j].down = a[i + 1][j];
}
}
}
【问题讨论】:
-
您之前的代码使用数组?数组是你能想象到的最原始的collection!而且...您将代码移到更高的级别,并且它不起作用?真的很抱歉……
-
@zlakad....你对让它工作有什么建议吗?这些是家庭作业实验室,我与整个班级都经历了巨大的斗争。我的 java 缺乏,现在比 3 个月前好,但对于我的生活......我不知道。我有一种唠叨的感觉,它与链接列表的构造有关。节点构造()。
标签: java matrix nullpointerexception linked-list