Home > Mobile >  How do you access int arrays in java methods
How do you access int arrays in java methods

Time:05-03

I'm trying to declare a 2D array of integers, set its size in the constructor, and change the array's values in a method. When I compile this , I get "Cannot store to int array because "this.a" is null". I'm not sure what I'm doing wrong.

public class arrays {
    int[][] a;

    public arrays(){
        int[][] a = new int[10][10];
    }
    public void m(){
        a[0][0] = 1;
        System.out.println(a[0][0]);
    }
    public static void main(String[] args){
        arrays Ar = new arrays();
        Ar.m();
    }
}

CodePudding user response:

int[][] a = new int[10][10];

This is a combination of int[][] a; and a = new int[10][10]; - int[][] a; declares a new variable. It has the same name as your field, and thus now two different things, both named a, are in 'scope'; the local variable 'wins' - a = new int[10][10]; creates a new array and then assigns a reference to it to your local variable named a, not to the field named a. The constructor then ends, and all local variables are tossed away, because that's what always happens to local variables once a method/constructor exits: They disappear. In other words, you've created a new array but nothing is referring to; your field named a is not pointing at it because you didn't change that.

Just a = new int[10][10]; will get the job done, as now there is no local variable also named a and thus a now refers to the field you have.

Or, even simpler, get rid of your constructor entirely, and initialize your array as you declare it:

public class Arrays {
    int[][] a = new int[10][10];
    
    public void m() {
        a[0][0] = 1;
        System.out.println(a[0][0]);
    }

    public static void main(String[] args) {
        Arrays ar = new Arrays();
        ar.m();
    }
}

CodePudding user response:

It is all about scoping.

1- You have a property called int[][] a in your class.

2- You have another int[][] a, which is a local variable in your constructor because you defined another int[][].

So, as @Turing85 said,just remove int[][] type definition inside your constructor and that line should be a = new int[10][10];

Additional information:

When you define another int[][] a in your constructor, there is more than one a. While assigning new int[10][10] to a, the closest one in terms of scope(which is inside the constructor) is used. It may be useful for you to exercise scoping.

CodePudding user response:

You have to declare the size before you are initializing the array

public class CustomArrays {
    int[][] a = new int[10][10];

    public void m(){
        a[0][0] = 1;
        System.out.println(a[0][0]);
    }
    public static void main(String[] args){
        CustomArrays Ar = new CustomArrays();
        Ar.m();
    }
}

or this

public class CustomArrays {
    int[][] a = new int[10][10];

   CustomArrays() {
      a[0][0]=1;
   }

    public void m(){  
        System.out.println(a[0][0]);
    }
    public static void main(String[] args){
        CustomArrays Ar = new CustomArrays();
        Ar.m();
    }
}

and

public class CustomArrays {
    int[][] a;

    public CustomArrays() {
         a = new int[10][10];
         a[0][0] = 1;
    }

    public void m() {

        System.out.println(this.a[0][0]);
    }

    public static void main(String[] args) {
        CustomArrays Ar = new CustomArrays();
        Ar.m();
    }
}

all work.

  • Related