英文:
Why is global object array suddenly null when it enters a method?
问题
为什么一个非空的全局数组会在方法中突然变成 null?
我尝试将该方法移入对象类并通过那种方式访问它,因为 getWeight() 方法运行良好,但是我遇到了相同的问题。一旦我进入 viablePaths 方法,全局数组就变成了 null。我正在使用 eclipse 切换断点,我可以在进入方法之前看到数组中的所有内容。
我希望该方法能够访问 paths 和 archs 数组并返回一个整数值。根据当前数组的内容,该方法应将 pathIndex 整数变量设置为 -1。我不知道还应该为所需行为添加什么。期望的行为是,当调用方法时,全局对象数组不会变为 null。
目前我遇到的错误是,在调用 determineViablePaths 方法的那一行出现了 NullPointerException。
public class StackHelpProblem {
public static int numEdges;
public static int queries;
public static Edge[] paths;
public static Edge[] archs;
public static void main(String[] args) {
numEdges = 3;
queries = 2;
Edge[] paths = new Edge[numEdges];
paths[0] = new Edge(1);
paths[1] = new Edge(2);
paths[2] = new Edge(3);
Edge[] archs = new Edge[queries];
archs[0] = new Edge(10);
archs[1] = new Edge(10);
int pathIndex = 0;
// Reachable artifacts set
for (int i = 0; i < queries; i++) {
System.out.println(archs[i].getWeight());
pathIndex = determineViablePaths(archs[i].getWeight());
}
}
// Method to return last index of path that can be traversed
public static int determineViablePaths(int weight) {
for (int i = 0; i < numEdges; i++) {
if (paths[i].getWeight() <= weight)
continue;
else {
return i - 1;
}
}
return numEdges - 1;
}
// The edge class
public static class Edge implements Comparable<Edge> {
int weight;
Edge(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
// The Edge comparison method
public int compareTo(Edge other) {
if (this.weight < other.weight)
return 0;
else if (this.weight == other.weight)
return 1;
return -1;
}
}
}
getWeight() 方法有效,但 determineViablePath(int weight) 方法无效。一旦进入该方法,全局数组就变成 null。
感谢您的任何帮助。
英文:
Why would a non-null global array suddenly become null in a method?
I have tried to move the method into the object class and access it that way since the getWeight() method works fine, but I have the same issue. As soon as I step into the viablePaths method the global arrays are null. I am using eclipse to toggle breakpoints and I can see everything in the arrays before I step into the method.
I would like the method to be able to access the paths and archs arrays and return an integer value. With the content of the arrays right now the method should set the pathIndex integer variable to -1. I do not know what else to put for the desired behavior. The desired behavior should be for the the global object arrays to not become null when the method is called.
Currently the error I get is a NullPointerException from the line where the determineViable paths method is called.
public class StackHelpProblem {
public static int numEdges;
public static int queries;
public static Edge[] paths;
public static Edge[] archs;
public static void main(String[] args) {
numEdges = 3;
queries = 2;
Edge[] paths = new Edge[numEdges];
paths[0] = new Edge(1);
paths[1] = new Edge(2);
paths[2] = new Edge(3);
Edge[] archs = new Edge[queries];
archs[0] = new Edge(10);
archs[1] = new Edge(10);
int pathIndex = 0;
// Reachable artifacts set
for (int i = 0; i < queries; i++) {
System.out.println(archs[i].getWeight());
pathIndex = determineViablePaths(archs[i].getWeight());
}
}
// Method to return last index of path that can be traversed
public static int determineViablePaths(int weight) {
for (int i = 0; i < numEdges; i++) {
if (paths[i].getWeight() <= weight)
continue;
else {
return i - 1;
}
}
return numEdges-1;
}
// The edge class
public static class Edge implements Comparable<Edge> {
int weight;
Edge(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
// The Edge comparison method
public int compareTo(Edge other) {
if(this.weight < other.weight)
return 0;
else if (this.weight == other.weight)
return 1;
return -1 ;
}
}
}
the getWeight() works and the determineViablePath(int weight) method does not work. As soon as that method is entered the global arrays become null.
Thank you for any assistance.
答案1
得分: 2
让我们分析代码中的相关部分,以了解为什么会引发“Exception”,以及如何防止它。
public class StackHelpProblem {
...
public static Edge[] paths;
public static Edge[] archs;
...
}
两个静态字段 Edge[] paths
和 Edge[] archs
没有被初始化,因此会隐式地被初始化为 null
。
public class StackHelpProblem {
...
public static void main(String[] args) {
...
Edge[] paths = new Edge[numEdges];
...
Edge[] archs = new Edge[queries];
...
}
...
}
在方法 static void main(...)
中,创建了两个新的数组 paths
和 archs
。它们不会引用静态字段。因此,静态字段仍然会被初始化为 null
。
在方法 static int determineViablePaths(...)
中,没有找到名为 paths
的局部变量,因此使用了静态字段 paths
,而该字段仍然为 null
。因此,数组访问将导致“NullPointerException”。
我们可以通过在方法 static void main(...)
中使用现有的静态字段,而不是创建新的变量,来消除“NullPointerException”:
public class StackHelpProblem {
...
public static void main(String[] args) {
...
paths = new Edge[numEdges];
...
archs = new Edge[queries];
...
}
...
}
英文:
Let us analyze the relevant parts of the code to understand why the Exception
is thrown and how we can prevent it.
public class StackHelpProblem {
...
public static Edge[] paths;
public static Edge[] archs;
...
}
The two static fields Edge[] paths
and Edge[] archs
are not initialized and thus implicitly initialized with null
.
public class StackHelpProblem {
...
public static void main(String[] args) {
...
Edge[] paths = new Edge[numEdges];
...
Edge[] archs = new Edge[queries];
...
}
...
}
In method static void main(...)
, two new arrays paths
and archs
are created. Those do not reference the static fields. Thus, the static fields are still initialized with null
.
In method static int determineViablePaths(...)
, no local variable paths
is found, thus static field paths
is used, which still is null
. Thus, the array-access will result in a NullPointerException
.
We can get rid of the NullPointerException
by using the existing static fields instead of creating new variables in method static void main(...)
:
public class StackHelpProblem {
...
public static void main(String[] args) {
...
paths = new Edge[numEdges];
...
archs = new Edge[queries];
...
}
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论