0

I am trying to convert a string matrix to a double matrix, but keep on getting this error:

Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at wim_data_reader.WIMdataReader.main(WIMdataReader.java:135)

Each row in the matrix is not necessarily the same size as the previous row. Here is my code:

 //double matrix to store all the converted string elements
 double[][] data = new double[WIMdataMatrix.length][];

 //double for loop used to convert string elements to double elements and store them in matrix
 for (int i = 0; i < WIMdataMatrix.length; i++) {

     //
     data[i] = new double[WIMdataMatrix[i].length];

     for (int j = 0; j < WIMdataMatrix[i].length; j++) {

         data[i][j] = Double.parseDouble(WIMdataMatrix[i][j]);

     }
 }   

Please help!

  • I have read that article and I understand what a NullPointerException is. That is why I create a new row length everytime the first for loop starts again, so that it does not point to a Null element. But I am not sure if I am missing something or what I am doing wrong. – Gilmore Munro Sep 07 '17 at 12:53
  • You've read that question as well, good. Have you also read the stacktrace? Have you seen that `WIMdataMatrix[i][j]` is `null`? It is ___impossible___ for us to know why, since we know nothing about that array. – Tom Sep 07 '17 at 12:57
  • Can you check that `WIMdataMatrix[i][j]` is not null? Maybe put a sysout there in that case, or throw a custom exception. `Double.parseDouble()` will throw a NPE if you pass a null string. If your row was null, the stacktrace would start at that row, not somewhere inside `parseDouble()`. – Malte Hartwig Sep 07 '17 at 12:58
  • Thanks guys, I got it working! – Gilmore Munro Sep 07 '17 at 13:38

0 Answers0