I need to assign a value to my 2 Dimension array.
I tried to code as below, but I get NullPointer Exception Error.
MethodClass[][] methodSet = new MethodClass[1][1];
methodSet[0][0].setmethodName(1);
methodSet[0][1].setmethodStatus(1);
The MethodClass file:
public class MethodClass {
private int methodName;
private int methodStatus;
public MethodClass() {
methodName = 0;
methodStatus = 0;
}
public int getmethodName() {
return methodName;
}
public int getmethodStatus() {
return methodStatus;
}
public void setmethodName(int i) {
this.methodName = i;
}
public void setmethodStatus(int status) {
this.methodStatus = status;
}
}
May I know how to initialize the value to 2 dimension array?
Mfor the getters & setters.MethodClass[][] methodSet = new MethodClass[1][1];is perfectly fine initialization, but I assume that you want to fill your 2D array with objects - that's a different thing. After you initialize the 2D array it's filled withnull. Now you want to create objects and assign them into the array.methodSet[i][j] = new MethodClass()first[0][1]on an array of size[1][1].. you'll getArrayIndexOutOfBoundsException