kevin shen
kevin shen

Reputation: 61

Exception in main thread. Do not know the reason

package examples;
import java.util.Scanner;

public class MatrixMultiplication {
    public static void main(String[] args) {

The below 4 sections identifies user input for the rows and columns of two matrices.

        Scanner userrows1 = new Scanner(System.in);
        System.out.println("Enter number of rows for matrix 1: ");
        int rows1 = userrows1.nextInt();

        Scanner usercolumns1 = new Scanner(System.in);
        System.out.println("Enter number of columns for matrix 2"); 
        int columns1 = usercolumns1.nextInt();

        Scanner userrows2 = new Scanner(System.in);
        System.out.println("Enter number of rows for matrix 2: ");
        int rows2 = userrows2.nextInt();

        Scanner usercolumns2 = new Scanner(System.in);
        System.out.println("Enter number of columns for matrix 2"); 
        int columns2 = usercolumns2.nextInt();

This sets the objects matrix1 and matrix2 as belonging to the class Matrix

        Matrix matrix1 = new Matrix(rows1, columns1);
        Matrix matrix2 = new Matrix(rows2, columns2);

        matrix1.ShowMatrix();
        System.out.println("\n \n");

        matrix2.ShowMatrix();

    }

}


class Matrix {

    int rows;
    int columns;
    int[][] values;

    public Matrix(int r, int c) {
        rows = r;
        columns = c;

        int[][] values = new int[r][c]; 

This originally served to allow the user to input values of a matrix one by one. For now I just set all values of the matrix to a certain value for simplicity.

        int i;
        int j;
        for(i = 0; i < r; i++) {
            for(j = 0; j < c; j++) {
                //Scanner userelement = new Scanner(System.in);
                //System.out.println("Enter number:");
                //int element = userelement.nextInt();
                values[i][j] = 1;

            }


        }



    }


    public void ShowMatrix() {
        int k;
        int l;
        for(k = 0; k < rows; k++) {
            for(l = 0; l < columns; l++) {
                System.out.println(values[k][l] + " ");

            }
            System.out.println("\n");

        }


    }


}


The code is above. In the final method in the class Matrix (the method is ShowMatrix), I am trying to print out the matrix. However, I am using the general values matrix here and it says:

Exception in thread "main" java.lang.NullPointerException
    at examples.Matrix.ShowMatrix(MatrixMultiplication.java:75)
    at examples.MatrixMultiplication.main(MatrixMultiplication.java:29)

Can anyone diagnose the issue? Much thanks as I'm still very new to Java. 

Upvotes: 0

Views: 73

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

You've not instantiate the field [][]values (There is a local declaration of int[][] values).

public Matrix(int r, int c) {
  rows = r;
  columns = c;

  int[][] values = new int[r][c]; <--  Remove this
  values = new int[r][c];
  ....
}

Upvotes: 1

Just remove the package line if you are using the terminal or command prompt.

package examples;

Working Code:

import java.util.Scanner;

public class MatrixMultiplication {
    public static void main(String[] args) {

        Scanner userrows1 = new Scanner(System.in);
        System.out.println("Enter number of rows for matrix 1: ");
        int rows1 = userrows1.nextInt();

        Scanner usercolumns1 = new Scanner(System.in);
        System.out.println("Enter number of columns for matrix 2"); 
        int columns1 = usercolumns1.nextInt();

        Scanner userrows2 = new Scanner(System.in);
        System.out.println("Enter number of rows for matrix 2: ");
        int rows2 = userrows2.nextInt();

        Scanner usercolumns2 = new Scanner(System.in);
        System.out.println("Enter number of columns for matrix 2"); 
        int columns2 = usercolumns2.nextInt();


        Matrix matrix1 = new Matrix(rows1, columns1);
        Matrix matrix2 = new Matrix(rows2, columns2);

        matrix1.ShowMatrix();
        System.out.println("\n \n");

        matrix2.ShowMatrix();

    }

}


class Matrix {

    int rows;
    int columns;
    int[][] values;

    public Matrix(int r, int c) {
        rows = r;
        columns = c;

        //int[][] values = new int[r][c]; 
        this.values =  new int[r][c];

        int i;
        int j;
        for(i = 0; i < r; i++) {
            for(j = 0; j < c; j++) {

                this.values[i][j] = 1;
                }
            }



    }


    public void ShowMatrix() {
        int k;
        int l;
        for(k = 0; k < this.rows; k++) {
            for(l = 0; l < this.columns; l++) {
                System.out.print(this.values[k][l] + " ");

            }
            System.out.println("\n");

        }


    }


}

One more suggestion is that there is no need of creating new instance/object for Scanner class for each row and column.

        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter number of rows for matrix 1: ");
        int rows1 = userInput.nextInt();


        System.out.println("Enter number of columns for matrix 2"); 
        int columns1 = userInput.nextInt();


        System.out.println("Enter number of rows for matrix 2: ");
        int rows2 = userInput.nextInt();


        System.out.println("Enter number of columns for matrix 2"); 
        int columns2 = userInput.nextInt();

Upvotes: 0

Related Questions