0

This is my first time ever using java, and I'm having some difficulty getting my code to work. I have two .java files, the main one and a test file.

This is the main code:

public class Matrix  {
    double[][] array;
    double[][] matrix;
    public Matrix(double[][] array) {
        double[][] matrix = new double[array.length][];
        for(int i=0; i<array.length; i++)
            matrix[i] = array[i].clone();
    }
    public double[] getRow(int rownum) {
        int x;
        for(x=0; x<matrix[rownum].length; x++){
            System.out.print(matrix[rownum][x]);    
        }
        System.out.println();
        return matrix[rownum];
    }
    public void swap(int rowA, int rowB) {
        double temp[] = matrix[rowA];
        matrix[rowA] = matrix[rowB];
        matrix[rowB] = temp;
    }
    public int maxRow(int column_num) {
        int y;
        int maxLoc;
        double maxVal;
Line 29 maxVal = matrix[0][column_num];
        maxLoc = 0;
        for(y=0; y<matrix.length; y++){
            if(maxVal<matrix[y][column_num]){
                maxVal = matrix[y][column_num];
                maxLoc = y;
            }
        }
        return maxLoc;
    }
    public void scaleRow(int rownum, double factor) {
        // multiply all elements of current matrix in row rownum by factor
        int z;
        for(z=0; z<matrix[rownum].length; z++){
            matrix[rownum][z] = matrix[rownum][z]*factor;
        }
    }
}

The test file code is this:

import java.util.Arrays;
public class MatrixTest {
  public static void main(String[] args) {
    /* test cases for Matrix class methods */
    double[][] matrixA = {{1,2,3,4},{2,4,6,8},{9,10,11,12}};
    double[][] matrixB = {{0.92,-900,44},{1201,18.264,-21.0},{0,0,0}};
    double[][] matrixC = {{1.5E-12,-6.034E2},{41.8,-125E-3}};
    Matrix One = new Matrix(matrixA);
    Matrix Two = new Matrix(matrixB);
    Matrix Three = new Matrix(matrixC);
    /* test whether or not maxRow works properly */
Line 12 if (One.maxRow(0) == 2) {
           System.out.println("Passed maxRow test for matrix One"); }
    else { System.out.println("Failed maxRow test for matrix One"); }
    if (Two.maxRow(1) == 1) {
           System.out.println("Passed maxRow test for matrix Two"); }
    else { System.out.println("Failed maxRow test for matrix Two"); }

    /* test whether or not scaleRow works properly */
    One.scaleRow(0,2.0);    // scale row 0 by 2.0
    if (Arrays.equals(One.getRow(0),matrixA[1])) {
           System.out.println("Passed scaleRow test for matrix One"); }
    else { System.out.println("Failed scaleRow test for matrix One"); }
    Two.scaleRow(2,12.608); // scale row 2 by 12.608
    if (Arrays.equals(Two.getRow(2),matrixB[2])) {
           System.out.println("Passed scaleRow test for matrix Two"); }
    else { System.out.println("Failed scaleRow test for matrix Two"); }
    One.scaleRow(0,0.5);  // scale row 0 by 0.5
    if (Arrays.equals(One.getRow(0),matrixA[0])) {
           System.out.println("Passed scaleRow test for matrix Three"); }
    else { System.out.println("Failed scaleRow test for matrix Three"); }

    /* test whether or not swap method works properly */
    One.swap(2,0);  // swap contents of Row 2 with Row 0
    if (Arrays.equals(One.getRow(0),matrixA[2]) && 
        Arrays.equals(One.getRow(2),matrixA[0])) {
           System.out.println("Passed swap test for matrix One"); }
    else { System.out.println("Failed swap test for matrix One"); }
    Two.swap(0,1); Two.swap(1,2); Two.swap(1,0); // fancy swap sequence
    if (Arrays.equals(Two.getRow(0),matrixB[2]) && 
        Arrays.equals(Two.getRow(2),matrixB[0])) {
           System.out.println("Passed swap test for matrix Two"); }
    else { System.out.println("Failed swap test for matrix Two"); }

    }
  }

I didn't make the Test file, it was given to us. We can not change anything in the test file, we just had to create our own Matrix file.

I can compile the code, and when I run it I get an error stating: "Exception in thread "main" java.lang.NullPointerException. At Matrix.maxRow(Matrix.java:29) and at MatrixTest.main(MatrixTest.java:12)"

I've tried to initialize the maxVal variable while declaring it. My code at that point looked like: "double maxVal = new double[matrix[0][column_num]];" but that wouldn't work, the error said that "double[] cannot be converted to double". I also tried just initializing it as "double maxVal = new double[-99999999];" but that didn't work either.

Like I said before, I'm very new to java, and probably am missing some very obvious things. I would appreciate any help you could offer!

CabooseMSG
  • 51
  • 10
  • 1
    You never initialize your `matrix` member variable, you're hiding it with a local variable called `matrix` in your constructor. – azurefrog Sep 01 '16 at 21:39
  • So I should do "double[][] matrix = new double[array.length][];" and all of that outside of the public Matrix part of the code? Or do I add something else? I'm sorry for the basic terms, I'm new to this. – CabooseMSG Sep 01 '16 at 21:47
  • No, you should do `this.matrix = ...` in the constructor. – user207421 Sep 01 '16 at 22:06
  • Okay, I added a "this." to "this.matrix[i]=array[i].clone();" but now I get a NullPointerException there, in line 8. – CabooseMSG Sep 01 '16 at 22:19
  • Nevermind, I understand now. I redundantly declared the matrix twice, changing "double[][] matrix = new double..." to "this.matrix = new double..." fixed it up! – CabooseMSG Sep 01 '16 at 22:26

0 Answers0