8

I'm trying to read a .csv file from my resource folder in my maven project. I've done it before like this:

BufferedReader reader = new BufferedReader(
new InputStreamReader(this.getClass().getResource("info.csv").openStream()));

CSVParser csvParser = new CSVParser(reader,
CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());

and it worked. and now in another project, I'm trying read my file from resources and I get NullPointerException.

The only thing that is different between these 2 projects is my packages.

This is for the one that works:

and This is the one that doesn't work:

What am I doing wrong?

1
  • After fix by @jens answer, sure that resource's file is exist at target folder. If not, compile before Commented Aug 21, 2018 at 12:15

1 Answer 1

8

Add a slash before the filename:

new InputStreamReader(this.getClass().getResource("/info.csv").openStream()));

without slash it looks in the same directory structure than your class is located

Sign up to request clarification or add additional context in comments.

1 Comment

@MikhailIonkin it is an maven Project and will be copied by maven

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.