MTT
MTT

Reputation: 5263

Exception in thread "Thread-***" java.lang.NullPointerException

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.

Upvotes: 0

Views: 2909

Answers (2)

Sage
Sage

Reputation: 15418

String line1 = match_dataset1.readLine();

The line1 is null here. The BufferedReader.readLine() method returns null if the end of the stream has been reached. Which means that your stream would be empty or you have already reached to the end. Try changing the for loop to a while loop:

String line1 = "";
while((line1 = match_dataset1.readLine())!=null)
{
    //  other code
}

Upvotes: 1

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

Read the doc for BufferedReader.readLine: "returns null if the end of the stream has been reached". You should check variable temp and if it is null, do not attempt to call split on it.

Upvotes: 2

Related Questions