DoubleMatrix类中的空指针异常错误

时间:2016-02-11 03:05:57

标签: java

我遇到了java问题。我收到此错误: 线程" main"中的例外情况显示java.lang.NullPointerException     在DoubleMatrix.getDim1Size(DoubleMatrix.java:28)     在Program3.main(Program3.java:16) 我不明白它在哪里

public class DoubleMatrix 
{
    private double[][] doubMatrix; 

    public DoubleMatrix(int firstDim, int secondDim, double upperLimit)
    {
        if(firstDim > 0 && secondDim > 0 && upperLimit > 0){
            firstDim = 1; 
            secondDim = 1;
            upperLimit = 1; 
        }
    }

    public DoubleMatrix(double[][] tempArray)
    {
        if(tempArray != null && tempArray.length != 0){
            for(int i =0; i < tempArray.length; i++) {
                doubMatrix = tempArray; 
            }
        }
        else{
            tempArray = new double[1][1]; 
        }
    }

    public int getDim1Size(){
        int firstDim1 = doubMatrix.length; 
        return firstDim1; 
    }
    public int getDim2Size(){
        int secondDim1 = doubMatrix[0].length; 
        return secondDim1;
    }

    private void makeDoubMatrix(int firstDim, int secondDim, double upperLimit){
        double[][] randomMatrix = new double[firstDim][secondDim]; 

        for(int row = 0; row < doubMatrix.length; row++) {
            for(int column = 0; column < doubMatrix[row].length; column++){
                doubMatrix[row][column] = (double)(Math.random() * 100); 
            }
        }

    }
    public DoubleMatrix addMatrix(DoubleMatrix arrayObj)
    {
        if(doubMatrix.length == arrayObj.doubMatrix.length && doubMatrix[0].length == arrayObj.doubMatrix[0].length){
            double[][] TotalTwoDimArray = new double[doubMatrix.length][doubMatrix[0].length];
            for(int row = 0; row < TotalTwoDimArray.length; row++){
                for(int column = 0; column < TotalTwoDimArray[row].length; column++){ 
                    TotalTwoDimArray[row][column] = doubMatrix[row][column] + arrayObj.doubMatrix[row][column]; 
                }
            }
            return new DoubleMatrix(TotalTwoDimArray); 
        }
        return new DoubleMatrix(1, 1, 1); 
    }
    public DoubleMatrix getTransposedMatrix(){
        double[][] TransMatrix = new double[doubMatrix[0].length][doubMatrix.length]; 
        for(int row = 0; row < doubMatrix.length; row++){
            for(int column = 0; column < doubMatrix[row].length; column++){
                TransMatrix[row][column] = doubMatrix[column][row];
            }
        }
        return new DoubleMatrix(TransMatrix); 
    }

    public DoubleMatrix multiplyMatrix(DoubleMatrix obj1)
    {
        if(doubMatrix[0].length == obj1.doubMatrix.length){
            double[][] multipliedMatrix = new double[doubMatrix.length][obj1.doubMatrix[0].length];

            for(int i = 0; i < multipliedMatrix.length; i++){
                for(int j = 0; j < multipliedMatrix[i].length; j++){
                    for(int k = 0; k < doubMatrix[0].length; k++){
                        multipliedMatrix[i][j] = doubMatrix[i][k] * obj1.doubMatrix[k][j] + multipliedMatrix[i][j]; 
                    }
                }
            }
            return new DoubleMatrix(multipliedMatrix); 
        }
        return new DoubleMatrix(1, 1, 1); 
    }
    public void printMatrix(String titles){
        System.out.println(titles);

        for(int row = 0; row < doubMatrix.length; row++){
            for(int column = 0; column < doubMatrix[row].length; column++){
                System.out.printf("%9.1f", doubMatrix[row][column]);
            }
            System.out.println();
        }
    }

}

// main in different class
public class Program3 
{
    public static void main(String[] args)
    {
        DoubleMatrix doubMatObj1; 
        DoubleMatrix doubMatObj2; 
        DoubleMatrix doubMatObj3; 

        int max = 10; 
        int min = 3; 
        int firstDim = (int)(Math.random() * (max - min + 1) + min);
        int secondDim = (int)(Math.random() * (max - min + 1) + min);  

        doubMatObj1 = new DoubleMatrix(firstDim, secondDim, 100.);  
        doubMatObj2 = new DoubleMatrix(doubMatObj1.getDim1Size(), doubMatObj1.getDim2Size(), 100.); 
        doubMatObj3 = doubMatObj1.addMatrix(doubMatObj2); 
        doubMatObj1.printMatrix("First Matrix Object"); 
        doubMatObj2.printMatrix("Second Matrix Object"); 
        doubMatObj3.printMatrix("Result of Adding Matrix Objects"); 
        doubMatObj2.printMatrix("Result of Transposing Matrix Object");
        doubMatObj1.multiplyMatrix(doubMatObj2); 
        doubMatObj3.printMatrix("Result of Multiplying Matrix Objects"); 
    }
}

1 个答案:

答案 0 :(得分:0)

在java中,非原语不会通过声明它们来初始化。因此,如果您在NullPointerException之类的行中获得foo.bar(),则您知道foo必须是null。在您的情况下,您有doubMatrix.length,表示doubMatrix从未初始化。查看代码,只有第二个构造函数初始化了该变量,因此调用第一个构造函数会使doubMatrix==null始终为true

我希望有足够的信息来帮助您自己解决问题(以及将来的类似问题),但我不打算发布一个有效的代码示例,因为自己修复代码将是一个很好的练习!

在旁注中,在第二个构造函数中,您有:

for(int i =0; i < tempArray.length; i++) {
    doubMatrix = tempArray; 
}

如果tempArray.length例如5,则会为同一个变量分配相同的值5次。我不知道你在那里做什么,但肯定不是你的想法。