0

I have two classes in a Maven project, which contain the same code (except for their name). The code shall later create a new class with Javassist based on a csv-file.

The first one CsvParser is placed in the src/main/java/csvParser package. The second one TestCsvParser is placed in the src/test/java/csvParser package. In both packages the same file assistant.csv is placed.

When I run the one from the main directory (CsvParser) I get a java.lang.NullPointerException but when I run TestCsvParser, placed in the testdirectory the same code works fine.

Why is it like that? (Or do I just not see something? ;) )

CsvParser:

package csvParser;    

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;    

public class CsvParser {    

    public static void main(String[] args) throws IOException
    {
        createClass("/assistant.csv");
    }    

    /**
     * Create a class from a csv-file.
     */
    private static void createClass(String input) throws IOException {
        try(BufferedReader stream = new BufferedReader(new InputStreamReader(
                CsvParser.class.getResourceAsStream(input))))
        {
            // Create class based on csv-file.
        }
    }
}

TestCsvParser:

package csvParser;    

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;    

public class TestCsvParser {    

    public static void main(String[] args) throws IOException
    {
        createClass("/assistant.csv");
    }    

    /**
     * Create a class from a csv-file.
     */
    private static void createClass(String input) throws IOException {
        try(BufferedReader stream = new BufferedReader(new InputStreamReader(
                TestCsvParser.class.getResourceAsStream(input))))
        {
            // Create class based on csv-file.
        }
    }
}

The exception

Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at csvParser.CsvParser.createClass(CsvParser.java:19)
    at csvParser.CsvParser.main(CsvParser.java:11)

I believe this question is not a duplicate of a question like What is a NullPointerException because: The NullPointerException occurs based on the location of the class and the resource referred to. So it's more about directory structures and Mavens targetdirectory.

Thanks for your time!

So S
  • 531
  • 1
  • 7
  • 18

2 Answers2

1

Finally I found the error. I added the assistant.csv beside the two classes (CsvParser and TestCsvParser). But in both cases this file is not added to the target directory.

The reason why it was working in TestCsvParser is an additional assistant.csv in the ../test/resource/ directory. In fact the two conditions which I described missed this fact and therefore you could not reconstruct my error fully. I'm sorry for that.

To have a working example the resource files both for main and test have to placed within the resource folder instead of beside the class.

Thanks for your help, especially Kalaiselvan A.

So S
  • 531
  • 1
  • 7
  • 18
0

It will depend on location of "/assistant.csv" file and if it's not found, you will get NPE. The path will be dependent on your class location since you are calling CsvParser.class.getResourceAsStream..

Optional
  • 4,081
  • 3
  • 22
  • 42
  • Maybe I missunderstand you, but the file is placed in both directories. Both times just beside the class. – So S Oct 10 '17 at 09:01
  • then where is NPE? I guess it is in finding the input resource... if not, at what line you see NPE? – Optional Oct 10 '17 at 09:01
  • @SoS To validate what I am saying try printing this. `CsvParser.class.getResource("/assistant.csv")` and you will know where it is null. – Optional Oct 10 '17 at 09:05
  • `CsvParser.class.getResource("/assistant.csv")` returns `null`. `TestCsvParser.class.getResource("/assistant.csv")` returns `file:/C:/Users/User/IdeaProjects/ComponentClassification/target/test-classes/assistant.csv`. So you are right, but that does not explain why it is like it is ;). – So S Oct 10 '17 at 09:17
  • it is very much like that, because your assitant.csv may not be getting copied to target/classes when you run the non test class. – Optional Oct 10 '17 at 09:19
  • @SoS If you see, the file is looked under test-classes for test and for main, it will be looked under `/target/classes/` or whatever is the class folder for main file. – Optional Oct 10 '17 at 09:20