65535
65535

Reputation: 553

Univocity Parsers - The code doesn't run: Exception in thread "main" java.lang.NullPointerException

I want to parse .csv files and I'm following the Univocity Parsers tutorial, and have added their jar file to the dependencies here.

A null pointer exception occurs.

I would like to get Univocity Parsers working so I can see how good it is, here is the code I am running in IntelliJ, any help would be greatly appreciated, thanks!

import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.IllegalStateException;
import java.lang.String;
import java.util.List;


public class UnivocityParsers {

public Reader getReader(String relativePath) {
    try {
        return new InputStreamReader(this.getClass().getResourceAsStream(relativePath), "Windows-1252");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("Unable to read input", e);
    }
}

public void parseCSV(){

    CsvParserSettings settings = new CsvParserSettings();
    //the file used in the example uses '\n' as the line separator sequence.
    //the line separator sequence is defined here to ensure systems such as MacOS and Windows
    //are able to process this file correctly (MacOS uses '\r'; and Windows uses '\r\n').
    settings.getFormat().setLineSeparator("\r");

    // creates a CSV parser
    CsvParser parser = new CsvParser(settings);

    // parses all rows in one go.
    List<String[]> allRows = parser.parseAll(getReader("prodlist.csv"));

}

public static void main(String arg[]) {

    UnivocityParsers univocityParsers = new UnivocityParsers();

    univocityParsers.parseCSV();


}


}

Stack trace:

 Exception in thread "main" java.lang.NullPointerException   
 at java.io.Reader.<init>(Reader.java:78)
 at java.io.InputStreamReader.<init>(InputStreamReader.java:97)
 at UnivocityParsers.getReader(UnivocityParsers.java:15)
 at UnivocityParsers.parseCSV(UnivocityParsers.java:33)
 at UnivocityParsers.main(UnivocityParsers.java:41)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:497)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

 Process finished with exit code 1

Upvotes: 1

Views: 1914

Answers (1)

Jeronimo Backes
Jeronimo Backes

Reputation: 6289

The InputStreamReader is not finding your "prodlist.csv" file. I suspect it should be "/prodlist.csv" as it seems you are getting this file from your resources folder.

In any case, you can use the full path to your file just to make sure. Add this method:

public Reader getFileReader(String absolutePath) {
    return new InputStreamReader(new FileInputStream(new File(absolutePath)), "UTF-8");
}

Then call parser.parseAll with:

 List<String[]> allRows = parser.parseAll(getFileReader("c:/path/to/prodlist.csv"));

Upvotes: 1

Related Questions