0

I'm having trouble with a java problem. I get this error: Exception in thread "main" java.lang.NullPointerException at DoubleMatrix.getDim1Size(DoubleMatrix.java:28) at Program3.main(Program3.java:16) I don't understand where it is null

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"); 
    }
}
  • is `doubMatrix` a typo, or did you call it that intentionally? – Neuron Feb 11 '16 at 03:31
  • I just noticed a bunch of other problems where your code is not doing what you had in mind. I hate to tell you, but you'll have to do quite some debugging after fixing that`NullPointerException`.. – Neuron Feb 11 '16 at 03:41

1 Answers1

0

In java, non primitives don't get initialized by just declaring them. So if you get a NullPointerException in a line like foo.bar(), you know that foo had to be null. In your case you have doubMatrix.length, which indicates that doubMatrix has never been initialized. Looking at your code, only the second constructor ever initializes that variable, so calling the first constructor will leave doubMatrix==null to always be true.

I hope that is enough info to help you fix your problem yourself (and similar problems in the future), but I am not going to post a working code example, since fixing your code yourself will be a good exercise!

On a sidenote, in your second constructor you have:

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

If tempArray.length is for example 5, you would assign the same value 5 times to the same variable. I don't know what you are trying to do there, but it is certainly not what you had in mind.

Neuron
  • 5,141
  • 5
  • 38
  • 59