[英]Java - Change values of an array by a method in a Class
Here is my code: 这是我的代码:
class Myclass {
private static int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
int[] array = new int[10];
}
}
It throws a java.lang.nullPointerException
when trying to do this: 尝试执行此操作时,它会抛出java.lang.nullPointerException
:
m.array[i] = i;
Can anybody help me please? 有人能帮帮我吗?
You have declared a local variable array
in your constructor, so you're not actually initializing the array
declared in Myclass
. 您已在构造函数中声明了一个局部变量array
,因此您实际上并未初始化Myclass
声明的array
。
You'll want to refer directly to array
in the constructor. 您将要在构造函数中直接引用array
。 Instead of 代替
int[] array = new int[10];
Use this 用这个
array = new int[10];
Additionally, you've declared array
static in the scope of your Myclass
class. 另外,您已在Myclass
类的范围内声明了array
static。
private static int[] array;
You only have one instance of Myclass
here, so it doesn't matter, but normally this would not be static, if you're initializing it in a constructor. 你在这里只有一个Myclass
实例,所以没关系,但通常这不是静态的,如果你在构造函数中初始化它。 You should remove static
: 你应该删除static
:
private int[] array;
In your constructor you are making your assignment to a local variable names array, not the static class variable also named array. 在构造函数中,您将分配给局部变量名称数组,而不是名为array的静态类变量。 This is a scope problem. 这是一个范围问题。
I'm also guessing that since you access array via m.array, you want a member variable and not a static one. 我也猜测,因为你通过m.array访问数组,你需要一个成员变量而不是静态变量。 Here's the fix 这是修复
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
rray = new int[10];
}
}
in MyClass()
type this 在MyClass()
键入此内容
this.array = new int [10];
instead of this 而不是这个
int[] array = new int[10];
Your code should be as below. 您的代码应如下所示。 In the constructor, instead of initializing the instance variable you created a new local variable and the instance variable was left uninitalized which caused the NullPointerException. 在构造函数中,不是初始化实例变量,而是创建了一个新的局部变量,而实例变量未被初始化,这导致了NullPointerException。 Also the instance variable shouldn't be static. 实例变量也不应该是静态的。
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
array = new int[10];
}
}
First, if you plan to use array
as a field of m
(ie m.array
) don't declare it as static
, but: 首先,如果您计划将array
用作m
的字段(即m.array
),请不要将其声明为static
,但是:
private int[] array;
Next thing you have to do is to initialize it. 接下来你要做的就是初始化它。 Best place to do that is in the constructor: 最好的地方是在构造函数中:
public MyClass() {
array= new int[10]; //just array = new int[10]; don't put int[] in front of the array, because the variable already exists as a field.
}
The rest of the code should work. 其余的代码应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.