Java 为什么全局对象数组在进入方法时突然为空?

Java 为什么全局对象数组在进入方法时突然为空?,java,arrays,Java,Arrays,为什么非null全局数组在方法中突然变为null 由于getWeight()方法工作正常,我尝试将该方法移动到对象类中并以这种方式访问它,但我也遇到了同样的问题。一旦我进入viablePaths方法,全局数组就为空。我正在使用eclipse切换断点,在进入该方法之前,我可以看到数组中的所有内容 我希望该方法能够访问路径和ARCH数组并返回一个整数值。对于数组的内容,该方法现在应该将pathIndex整数变量设置为-1。我不知道还有什么可以让你做出想要的行为。所需的行为应该是全局对象数组在调用方法

为什么非null全局数组在方法中突然变为null

由于getWeight()方法工作正常,我尝试将该方法移动到对象类中并以这种方式访问它,但我也遇到了同样的问题。一旦我进入viablePaths方法,全局数组就为空。我正在使用eclipse切换断点,在进入该方法之前,我可以看到数组中的所有内容

我希望该方法能够访问路径和ARCH数组并返回一个整数值。对于数组的内容,该方法现在应该将pathIndex整数变量设置为-1。我不知道还有什么可以让你做出想要的行为。所需的行为应该是全局对象数组在调用方法时不变为null

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 ;
            }
        }
}
当前,我得到的错误是来自调用determiniveviable paths方法的行的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 ;
            }
        }
}
公共类堆栈帮助问题{
公共静态整数;
公共静态int查询;
公共静态边缘[]路径;
公共静态边缘[]拱门;
公共静态void main(字符串[]args){
numEdges=3;
查询=2;
边[]路径=新边[numEdges];
路径[0]=新边(1);
路径[1]=新边(2);
路径[2]=新边(3);
Edge[]archs=新边[查询];
archs[0]=新边(10);
archs[1]=新边(10);
int路径索引=0;
//可达工件集
for(int i=0;iif(path[i].getWeight()让我们分析代码的相关部分,以了解引发异常的原因以及如何防止它

public class StackHelpProblem {
    ...
    public static Edge[] paths;
    public static Edge[] archs;
    ...
}
两个静态字段
Edge[]path
Edge[]arch
未初始化,因此隐式初始化为
null

public class StackHelpProblem {
    ...

    public static void main(String[] args) {
        ...
        Edge[] paths = new Edge[numEdges];
        ...
        Edge[] archs = new Edge[queries];
        ...
    }
    ...
}
在方法
static void main(…)
中,创建了两个新的数组
路径
ARCH
。它们不引用静态字段。因此,静态字段仍然使用
null
初始化

public class StackHelpProblem {
    ...

    public static void main(String[] args) {
        ...
        Edge[] paths = new Edge[numEdges];
        ...
        Edge[] archs = new Edge[queries];
        ...
    }
    ...
}
在方法
static int determiniveviablepaths(…)
中,未找到局部变量
paths
,因此使用静态字段
paths
,该字段仍然是
null
。因此,数组访问将导致
NullPointerException

通过使用现有的静态字段,而不是在方法
static void main(…)
中创建新变量,我们可以消除
NullPointerException


让我们分析代码的相关部分,以了解引发异常的原因,以及我们如何防止它

public class StackHelpProblem {
    ...
    public static Edge[] paths;
    public static Edge[] archs;
    ...
}
两个静态字段
Edge[]path
Edge[]arch
未初始化,因此隐式初始化为
null

public class StackHelpProblem {
    ...

    public static void main(String[] args) {
        ...
        Edge[] paths = new Edge[numEdges];
        ...
        Edge[] archs = new Edge[queries];
        ...
    }
    ...
}
在方法
static void main(…)
中,创建了两个新的数组
路径
ARCH
。它们不引用静态字段。因此,静态字段仍然使用
null
初始化

public class StackHelpProblem {
    ...

    public static void main(String[] args) {
        ...
        Edge[] paths = new Edge[numEdges];
        ...
        Edge[] archs = new Edge[queries];
        ...
    }
    ...
}
在方法
static int determiniveviablepaths(…)
中,未找到局部变量
paths
,因此使用静态字段
paths
,该字段仍然是
null
。因此,数组访问将导致
NullPointerException

通过使用现有的静态字段,而不是在方法
static void main(…)
中创建新变量,我们可以消除
NullPointerException


“为什么一个非空全局数组突然对一个方法来说是空的?”-我愿意打赌这不是正在发生的事情。请提供一个。你确定
archs[I]
null
。很可能你有
archs[I].getQueryIndex()
返回一个
整数
,转换为
int
失败,因为该方法返回
null
。将变量声明为final。设置字段时,您应该会收到编译错误提示。您初始化数组了吗?它们是null@SilviuBurcea如问题中所述,它们是(而且必须是)初始化,因为对
getWeight()
的调用起作用。我几乎100%确定问题在于对
int
的转换,而这个问题在理解
NullPointerException
时是一个混乱的问题,“为什么非null全局数组对方法突然显示为null?”-我愿意用大量的钱打赌这不是正在发生的事情。请提供一个。你确定
archs[I]
null
。很可能你有
archs[I]。getQueryIndex()
返回一个
整数
,转换为
int
失败,因为该方法返回
null
。将变量声明为final。设置字段时,您应该会收到编译错误提示。您初始化数组了吗?它们是null@SilviuBurcea如问题中所述,它们是(而且必须是)初始化,因为对
getWeight()
的调用有效。我几乎100%确定问题是对
int
的转换,而这个问题是理解
NullPointerException
的一个混淆。