0

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?

  • proper camel-case would be the have a capital `M` for the getters & setters. – Martin Zeitler Nov 21 '18 at 03:52
  • `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 with `null`. Now you want to create objects and assign them into the array. – Nir Alfasi Nov 21 '18 at 03:53
  • 2
    do `methodSet[i][j] = new MethodClass()` first – Kartik Nov 21 '18 at 03:53
  • also, you can't access `[0][1]` on an array of size `[1][1]`.. you'll get `ArrayIndexOutOfBoundsException` – Kartik Nov 21 '18 at 03:58

5 Answers5

1

The NullPointerException occurs when you try to access a member of a class but the instance of that class is itself containing null value.

To prevent NullPointerException in your case, you must initialize the array values, as for Object type array the default value in positions would be NULL.

Better to do:

    MethodClass[][] methodArray = new MethodClass[1][1]; //You can put any dimentions to this array, below for loop will initialize all the positions.

    for (int i = 0; i < methodArray.length; i++){
        for (int j = 0; j < methodArray[i].length; j++) {
            methodArray[i][j] = new MethodClass();
        }
    }

Then you can access your methods as below:

  methodArray[0][0].setmethodName(1);
  methodArray[0][0].setmethodStatus(1);
  • It helps me a lot... I have to add this methodSet[0][0]=new MethodClass(); Followed by methodArray[0][0].setmethodName(1); methodArray[0][0].setmethodStatus(1); –  Nov 22 '18 at 01:16
0

MethodClass[][] methodSet = new MethodClass[1][1] is only initializing your array. To initialize your elements in array you need to do the below code.

MethodClass[][] methodSet = new MethodClass[1][1];
methodSet[0][0]=new MethodClass();
methodSet[0][0].setmethodName(1);

Secondly, for methodset[0][1] you need to change the size or else it will throw ArrayIndexOutOfBoundsException.

 MethodClass[][] methodSet = new MethodClass[1][2];
Kartik
  • 7,677
  • 4
  • 28
  • 50
Pooja Aggarwal
  • 1,163
  • 6
  • 14
  • Hi Kartik, I already change the size to : MethodClass[][] methodSet = new MethodClass[1][2]; methodSet[0][0]=new MethodClass(); methodSet[0][0].setmethodName(1); methodSet[0][1]=new MethodClass(); methodSet[0][1].setmethodName(1); But I still get : org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jersey-servlet] in context with path [/AdaptiveAuthWSApps] threw exception [java.lang.ArrayIndexOutOfBoundsException: -1] with root cause java.lang.ArrayIndexOutOfBoundsException: -1 –  Nov 21 '18 at 09:08
  • Add your complete error stack trace. – Pooja Aggarwal Nov 21 '18 at 09:11
0

You've initialized the 2D array as 1x1 which means there will only be one element in the array. Trying to use index 1 will throw an error [indexing starts from 0].

If you want to go there, initialize it as a 2x2 array.

You're basically going out of bounds, so if it were an array, you'd get an ArrayIndexOutOfBoundsException.

Initialize it like :

MethodClass[][] methodset = new MethodClass[2][2];
Raghav Kukreti
  • 552
  • 5
  • 18
0

You are declaring a matrix of 1*1, which is of size one. There is only index as (0,0). (0,1) is invalid.

Try using the below code,

MethodClass.java

public class MethodClass {
    private int methodName;
    private int methodStatus;

    public MethodClass() {
        methodName = 0;
        methodStatus = 0;
    }

    public MethodClass(int methodName, int methodStatus) {
        this.methodName = methodName;
        this.methodStatus = methodStatus;
    }

    public int getMethodName() {
        return methodName;
    }

    public void setMethodName(int methodName) {
        this.methodName = methodName;
    }

    public int getMethodStatus() {
        return methodStatus;
    }

    public void setMethodStatus(int methodStatus) {
        this.methodStatus = methodStatus;
    }

}

Test.Java

public class Test {
    public static void main(String a[]){
        MethodClass[][] methodSet = new MethodClass[2][2];
        methodSet[0][0] = new MethodClass(0, 0);
        methodSet[0][1] = new MethodClass(0, 1);
        methodSet[1][0] = new MethodClass(1, 0);
        methodSet[1][1] = new MethodClass(1, 1);
        System.out.println(methodSet);
    }
}
Nithyananth
  • 74
  • 10
0

Null pointer exception shows when you try to access a method/function of a class but the instance of that class is itself containing null value. for example null.setmethodName(1) will throw exception.

The work around in your case is, First initialize your MethodClass 2D array with the constructor of the class so that, each cell of the 2D array contains MethodClass instance.

MethodClass[][] methodSet = new MethodClass[1][1];
//since the array size of each cell is fixed to 1 in 
//your case. So you can loop for row and column from 0 to less then 1 for 0 
//indexed cells. otherwise you could also use i<methodSet.length for row and
//j<methodSet[i].length for column
for(int i=0;i<1;i++){ 
 for(int j=0;j<1;j++){
     methodSet[i][j]=new MethodClass(); // your MethodClass constructor
 }
}

Then you can access your method/function as below:

methodSet[0][0].setmethodName(1); // since the size of each cell of the 2D array is one in your case and index starts from zero so only cell [0][0] is valid in this case
methodSet[0][0].setmethodStatus(1);
Rashedul.Rubel
  • 3,446
  • 25
  • 36