0

I am trying to create a 2-dimensional array without using ArrayList. I was successful at creating the array but I run into a snag when trying to print the array from another method because the array is not static. However, I cannot make the array static because I am also allowing the user to decided the size of the array. I thought I could override the array and then print it but that only returns null values. Should this be done with a variable-length argument?

public class Array {
public static int[][] array1;     


    public static void makeArray(){

      int [][] array1 = new int [Menu.arrayRow][Menu.arrayCol];
        for (int i = 0; i < array1.length; i++) {
             for (int j = 0; j < array1[0].length; j++) {
                  array1[i][j] = (i * array1[0].length) + j + 1;
             }
        }   
    }// end of makeArray


    public  static void displayArray(){

                for (int i = 0; i < array1.length; i++) {
             for (int j = 0; j < array1[0].length; j++) {
                  array1[i][j] = (i * array1[0].length) + j + 1;
                  System.out.print(array1[i][j] + ", ");
             }
             System.out.println("");
        }

    }// end of displayArray   

This is the result of running the program and choosing item #11 from my menu to display the array:

run:
Enter the number of rows for the array:5
Enter the number of columns for the array:5
========================================



    1. Insert a Row
    2. Insert a Column 
    3. Swap rows
    4. Swap columns
    5. Swap Upper and Lower triangles
    6. Display the triangles
    7. Reverse the contents of a column
    8. Reverse the diagnal
    9. Swap diagnals
    10. Display a subsection of the current 2D array
    11. Display the array
    12. Exit
    Choose an option from the menu :
11
========================================

enter image description here

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Pass the array itself to the method manipulating the array – bigbounty Mar 13 '18 at 02:20
  • Please read through [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). (don't want to close for duplicate though, still specific enough in my opinion) – Zabuzard Mar 13 '18 at 02:20
  • Passing the array to the method a likely getting me closer to resolution. – Elbowgrease Mar 13 '18 at 02:58

2 Answers2

2

You are redeclaring the array again in the create method scope:

int [][] array1 = ...

Just remove that:

array1 = ...

To access the class level array.

When you declare a local variable, you will access that in the makeArray method, and not the static field. Then, the field is still null when you call the next method. Beware not to 'shade' class variables with local variables.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
  • Isn't the class level array null by this definition? I feel like you're on to something, but I may be missing the point. – Elbowgrease Mar 13 '18 at 02:44
  • The class level array is null because it's never set; the `makeArray` method I assume you are calling is actually declaring a new local variable and assigning a new array to that, leaving the class level array null. That's why you get a null pointer on `displayArray`. Either change it as I proposed, so that `makeArray` and `displayArray` access the same array (class level), or, as per suggested by Elliott, make every method take or return the array, and hold its reference on the caller. – Luan Nico Mar 13 '18 at 02:52
0

Return the array from makeArray and pass the array to displayArray. I would also pass the row and col counts as arguments to the create. Like,

public class Array {
    public static int[][] makeArray(int row, int col) {
        int[][] array1 = new int[row][col];
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                array1[i][j] = (i * array1[i].length) + j + 1;
            }
        }
        return array1;
    }// end of makeArray

    public static void displayArray(int[][] array1) {
        System.out.println(Arrays.deepToString(array1));
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • passing the area seems like the right move to the indexes to be printed, but deeptoString isn't working. I think a for loop would still work within the displayArray method once the array is passed, but is there another way to call that method to print? – Elbowgrease Mar 13 '18 at 02:50
  • @Elbowgrease `deepToString` was just an example (it's built-in). **how** are you calling it? you didn't post that code. Save the array returned in `makeArray`, pass **that array** to `displayArray`. – Elliott Frisch Mar 13 '18 at 02:59
  • Array.displayArray(int [ ][ ] array 1); I have not tried printing with a passed array in this manner before – Elbowgrease Mar 13 '18 at 03:02
  • `Array.displayArray(array1);` assuming one of the previous lines is something like `int[][] array1 = Array.makeArray(5, 3);` it is the same as passing any other kind of variable to a method. – Elliott Frisch Mar 13 '18 at 03:09