简体   繁体   English

NullPointerException被抛出,但我不知道如何解决它。

[英]A NullPointerException is being thrown but I can't figure out how to fix it.

I have the following code. 我有以下代码。 It is a constructer for a Matrix class. 它是Matrix类的构造方法。 The main thread only calls the constructor, and then prints the matrix (I made a method for it). 主线程只调用构造函数,然后打印矩阵(我为此做了一个方法)。

public class Matrix{ 
    float [][] mainMatrix; 
    int rows; 
    int columns; 
    public Matrix(){
        System.out.printf("\nInput the number of rows \n") ;
    String rowR = new Scanner(System.in).next().replace(" ", "").replace("\n", "").toString();
    rows = Integer.parseInt(rowR);
    System.out.printf("\n Input the number of columns \n");
    String columnR = new Scanner(System.in).next().replace(" ", "").replace("\n", "").toString();
    columns = Integer.parseInt(columnR);
    System.out.printf("Input the rows, seperated by a \" \". Close the row with a \"]\"");
    System.out.println("Warning!!! Inproper input will cause the program to crash!"); 
    for(int r = 0; r < rows; r++){
        Scanner item = new Scanner(System.in);
        System.out.printf("\n["); 
        String[] raw = item.next().replace("]", "").replace("\n", "").split(" ");
        for(int c = 0; c < columns; c++){
            mainMatrix[r][c] = Float.parseFloat(raw[c]);  
        }
    }
    /*Bunch of methods*/ 
} 

For some reason, when the code is run, it returns a NullPointerException, and points to the line: 由于某种原因,在运行代码时,它将返回NullPointerException并指向该行:

mainMatrix[r][c] = Float.parseFloat(raw[c]);

If it helps, the output looks like this: 如果有帮助,输出如下所示:

 Input the number of columns 
2

Input the rows, seperated by a " ". Close the row with a "]"Warning!!! Inproper input will cause the program to crash!

[ 2 3] /*\n*/ 
[Ljava.lang.String;@29173efException in thread "main" java.lang.NullPointerException
    at mathProgs.linProg.Matrix.<init>(Matrix.java:51)
    at mathProgs.linProg.MatrixTest.main(MatrixTest.java:10)

the 2, 3, and ] are user inputs. 2、3和]是用户输入。 Enter is pressed after the "]" 在“]”后面按Enter

The reason is that you haven't initialized mainMatrix . 原因是您尚未初始化mainMatrix You need something like: 您需要类似:

mainMatrix = new int[rows][columns];

Otherwise the variable has its default value of null , so when you try to dereference it (assigning a value to an element of the array) you get the NullPointerException . 否则,变量的默认值将为null ,因此,当您尝试对其进行取消引用(将值分配给数组的元素 )时,会收到NullPointerException

Note that unlike in some other languages, once you've created an array object it has a fixed size - you can't just add items to it later. 请注意,与其他某些语言不同,创建数组对象后,它的大小将固定不变-您以后不能再向其中添加项目。 For that, you'd need a List implementation such as ArrayList . 为此,您需要一个List实现,例如ArrayList Not a problem in this case, as you know how many rows and columns you've got to start with - but worth bearing in mind. 在这种情况下,这不是问题,因为您知道必须从几行和几行开始-但要牢记。

You haven't initialized your mainMatrix attribute so its default value will be null thus getting the NPE when using it. 您尚未初始化mainMatrix属性,因此其默认值将为null从而在使用它时获得NPE。 Initialize it when you have your row and column variables: 在具有行和列变量时对其进行初始化:

mainMatrix = new float[rows][columns];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM