I have a class to read a text file and put everything in an array. Following is the code I wrote and it ias working fine:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class PSResidualReduction {
public PSResidualReduction() throws FileNotFoundException, IOException {
BufferedReader match_dataset1 = new BufferedReader(new FileReader("/home/mt.txt"));
String[] temp;
String delims = ",";
double[][] mt;
mt = new double[10][5];
for (int k1 = 0; k1 < 10; k1++) {
String line1 = match_dataset1.readLine();
temp = line1.split(delims);
for (int k2 = 0; k2 < 5; k2++) {
mt[k1][k2] = Double.parseDouble(temp[k2]);
}
}
match_dataset1.close();
}
}
Since the number of files to read is huge, I decided to use threads to make it faster. Here is the code after using threads:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PSResidualReduction1 implements Runnable {
@Override
public void run() {
BufferedReader match_dataset1;
try {
match_dataset1 = new BufferedReader(new FileReader("/home/mt.txt"));
String[] temp;
String delims = ",";
double[][] mt;
mt = new double[10][5];
for (int k1 = 0; k1 < 10; k1++) {
String line1 = match_dataset1.readLine();
temp = line1.split(delims);
for (int k2 = 0; k2 < 5; k2++) {
mt[k1][k2] = Double.parseDouble(temp[k2]);
}
}
match_dataset1.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PSResidualReduction1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PSResidualReduction1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and I got these errors:
Exception in thread "Thread-***" java.lang.NullPointerException
in some of the iterations. The error is from this line
temp = line1.split(delims);
I'm sure all input text files have the same format with the same number of columns and rows. P.S. I'm using netbeans.
BufferedReader#readLine()returnsnullwhen there are no more lines.