0

I have a class Matrix which contains the class definition:

import java.util.Scanner;

public class Matrix {

    private int[][] matrix;

    //construct matrix with given number of rows and columns
    public Matrix(int n, int m){
        matrix = new int[n][m];
    }

    //input values into matrix
    public void readMatrix(){
        Scanner input = new Scanner(System.in);
        for (int r = 0; r < matrix.length; r++){
            for (int c = 0; c < matrix[r].length; c++){
                matrix[r][c] = input.nextInt();
            }
        }
    }

    //display values of matrix
    public void displayMatrix(){
        for (int r = 0; r < matrix.length; r++){
            for (int c = 0; c < matrix[r].length; c++){
                System.out.print(matrix[r][c] + " ");
            }
            System.out.print("\n");
        }
    }
}

and a class MatrixApp which contains the main:

import java.util.Scanner;

public class MatrixApp {

    public static void main(String[] args) {
        Matrix m1 = null; //create matrix m1
        promptMatrix(m1); //ask for data for m1 from user
        m1.displayMatrix(); //display m1
    }

    private static void promptMatrix(Matrix m) {
        Scanner input = new Scanner(System.in);
        int rows, columns;
        //ask for number of rows
        System.out.print("Enter the number of rows: ");
        rows = input.nextInt();
        //ask for number of columns
        System.out.print("Enter the number of columns: ");
        columns = input.nextInt();
        //create matrix
        m = new Matrix(rows, columns);
        //ask for matrix data
        System.out.println("Enter matrix: ");
        m.readMatrix();
    }
}

I get the Null Pointer Exception error on m1.displayMatrix(). I guess that is because nothing has been stored in m1 although I have passed m1 to the method promptMatrix. Why is that and how can I solve that, please?

Saiyan
  • 197
  • 7
  • 22

3 Answers3

1

You have created your Matrix in the promptMatrix method, but remember that m is just a copy of the reference that was passed in initially -- null. You create the Matrix correctly, so that m refers to the new Matrix. But, m1 in main isn't changed -- it's still null.

Return m from promptMatrix and assign it to m1.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Create MatrixApp class object and invoke the promptMatrix().

 public class MatrixApp {

public static void main(String[] args) {
    MatrixApp m1 = new MatrixApp(); //create MatrixApp m1
    m1.promptMatrix();

}

private static void promptMatrix() {
    Scanner input = new Scanner(System.in);
    int rows, columns;
    //ask for number of rows
    System.out.print("Enter the number of rows: ");
    rows = input.nextInt();
    //ask for number of columns
    System.out.print("Enter the number of columns: ");
    columns = input.nextInt();
    //create matrix
    m = new Matrix(rows, columns);
    //ask for matrix data
    System.out.println("Enter matrix: ");
    m.readMatrix();
    m.displayMatrix(); //display m
}
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • You should give an explanation along with the solution so that he knows what his mistakes were and how they were fixed, working code may be helpful but it doesn't help make him a better programmer. – yitzih Sep 08 '15 at 21:51
0

Edit: I made an error as to where the error was occuring (my mistake, I didn't read it thoroughly enough. However what is written below is useful to understand how to better write code in an easy to read manner so I will leave it up.

<----Anything between this and the below marker isn't correct to solve this problem but helps understand what is further below which can have benefit so I am leaving it up---->

Well as you said m1 is null and you are passing it into a method therefor you are getting a NullPointerException. You would need to initialize the Matrix object through the new operator. Matrix m1 = new Matrix(n, m); //n and m are whatever values they need to be //since there is no default constructor However this isn't a good way to accomplish what you are trying to do.

<----Anything between this and the above marker isn't correct to solve this problem but helps understand what is further below which can have benefit so I am leaving it up---->

The correct way to do this is to have the method return a Matrix object instead of taking one.

Method would look like this:

public static Matrix promptMatrix() {
    //code to get n and m and set up matrix
    return new Matrix(n, m);
}

Then in your main method you would do:

Matrix m1 = promptMatrix(); 
yitzih
  • 3,018
  • 3
  • 27
  • 44