且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 Java 会在这里抛出 NullPointerException?

更新时间:2023-02-20 17:27:53

你所做的被称为 shadowing 你用局部变量 xx/代码>.所以你需要做的就是避免这种情况:

what you did is called shadowing you shadowed your field x with local variable x. so all you need to do is avoiding this:

int[] x = new int [N]; 是错误的,如果您希望字段初始化而不是局部变量,那么您可以执行以下操作: x = new int[N]; 更多信息请阅读 this

int[] x = new int [N]; is wrong, if you want your field to initialize instead of a local variable then you could do something like : x = new int [N]; for more information read this