0

Disclaimer: This is homework, so I am not looking for an exact answer because I'd like to do this on my own. I have to read a matrix from a text file that is filled with doubles and put into a 2D array. I'm currently struggling to find where exactly my problem lies in my code because I know how to use the built in Java Scanner for a normal array. My code below always give me a NullPointerException error which means my matrix 2D array is empty.

public Matrix(String fileName){
    //reads first double and puts it into the [1][1] category
    //then moves on to the [1][2] and so on
    //once it gets to the end, it switches to [2][1]
    Scanner input = new Scanner(fileName);
    int rows = 0;
    int columns = 0;
    while(input.hasNextLine()){
        ++rows;
        Scanner colReader = new Scanner(input.nextLine());
        while(colReader.hasNextDouble()){
            ++columns;
        }
    }
    double[][]matrix = new double[rows][columns];
    input.close();

    input = new Scanner(fileName);
    for(int i = 0; i < rows; ++i){
        for(int j = 0; j < columns; ++j){
            if(input.hasNextDouble()){
                matrix[i][j] = input.nextDouble();
            }
        }
    }
}

My text file:

1.25 0.5 0.5 1.25
0.5 1.25 0.5 1.5
1.25 1.5 0.25 1.25
Michael
  • 45
  • 5
  • Where does the NullPointerException occur? Look at the stack trace and see if you can pinpoint the line where the exception happens. – Defenestrator Sep 25 '17 at 18:04
  • 1
    @Ravi That question is very general and overly broad. I would discourage closing this question as a duplicate of that one. – Defenestrator Sep 25 '17 at 18:04
  • @Defenestrator the error occurs in my *toString()* function, which is the 1st of my nested for loop to see the 2d arrays length. – Michael Sep 25 '17 at 18:10
  • @azurefrog Forgot to change those back to hasNextDouble(), i was just testing. – Michael Sep 25 '17 at 18:13
  • 1
    @Michael there are no calls to *toString()* in your posted code. Does your posted code reproduce the error? – Defenestrator Sep 25 '17 at 18:15
  • You also still need to [edit] your question in include the stack trace (and indicate which line is throwing the NPE). – azurefrog Sep 25 '17 at 18:15
  • @Michael, you had a few important problems in your code - but it's not too hard to fix. I wrote a full solution to your question using your original code as-is with in-code comments. If it's useful and complete, could you please accept the answer by clicking on the gray check mark next to it, making it green? an up-vote would also be greatly appreciated :-) – Assafs Sep 26 '17 at 09:10

1 Answers1

1

Your code has a couple of errors - and since it's homework, I'll try to stick to your original design as much as possible:

public Matrix(String fileName){

    //First issue - you are opening a scanner to a file, and not its name.
    Scanner input = new Scanner(new File(fileName)); 
    int rows = 0;
    int columns = 0;

    while(input.hasNextLine()){
        ++rows;
        columns = 0; //Otherwise the columns will be a product of rows*columns
        Scanner colReader = new Scanner(input.nextLine());
        while(colReader.hasNextDouble()){
            //You must "read" the doubles to move on in the scanner.
            colReader.nextDouble();
            ++columns;
        }
        //Close the resource you opened!
        colReader.close();
    }
    double[][]matrix = new double[rows][columns];
    input.close();

    //Again, scanner of a file, not the filename. If you prefer, it's
    //even better to declare the file once in the beginning.
    input = new Scanner(new File(fileName));
    for(int i = 0; i < rows; ++i){
        for(int j = 0; j < columns; ++j){
            if(input.hasNextDouble()){
                matrix[i][j] = input.nextDouble();
            }
        }
    }
}

Please notice the comments - overall you were not far from a functional code, but the errors are important to understand.

Assafs
  • 3,257
  • 4
  • 26
  • 39