主线程中的异常。不知道原因

时间:2015-07-02 03:55:54

标签: java

package examples;
import java.util.Scanner;

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

以下4个部分标识了两个矩阵的行和列的用户输入。

        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();

这将对象matrix1和matrix2设置为属于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]; 

这最初用于允许用户逐个输入矩阵的值。为了简单起见,我现在只需将矩阵的所有值设置为某个值。

        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. 

2 个答案:

答案 0 :(得分:1)

您尚未实例化字段[][]values(有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];
  ....
}

答案 1 :(得分:0)

如果您使用终端或命令提示符,只需删除package行。

package examples;

工作代码:

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");

        }


    }


}

还有一个建议是,不需要为每个行和列为Scanner类创建新的实例/对象。

        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();