Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from src/main/resources gives NullPointerException

Tags:

java

maven

xls

In my Maven project, I have a xls file in src/main/resources. When I read it like this:

 InputStream in = new  FileInputStream("src/main/resources/WBU_template.xls"); 

everything is ok.

However I want to read it as InputStream with getResourceAsStream. When I do this, with or without the slash I always get a NPE.

     private static final String TEMPLATEFILE = "/WBU_template.xls";      InputStream in = this.getClass.getResourceAsStream(TEMPLATEFILE); 

No matter if the slash is there or not, or if I make use of the getClassLoader() method, I still get a NullPointer.

I also have tried this :

URL u = this.getClass().getResource(TEMPLATEFILE); System.out.println(u.getPath()); 

the console says.../target/classes/WBU_template.xls and then get my NullPointer.

What am I doing wrong ?

like image 732
dutchman79 Avatar asked Dec 20 '12 07:12

dutchman79


People also ask

How to find cause of NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How do I read a file from SRC main resources?

Using Java getResourceAsStream() This is an example of using getResourceAsStream method to read a file from src/main/resources directory. First, we are using the getResourceAsStream method to create an instance of InputStream. Next, we create an instance of InputStreamReader for the input stream.

How to avoid null pointer exception in java array?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result.

How do I give path to SRC main resources?

The simplest approach uses an instance of the java. io. File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file.


1 Answers

FileInputStream will load a the file path you pass to the constructor as relative from the working directory of the Java process.

getResourceAsStream() will load a file path relative from your application's classpath.

When you use .getClass().getResource(fileName) it considers the location of the fileName is the same location of the of the calling class.

When you use .getClass().getClassLoader().getResource(fileName) it considers the location of the fileName is the root - in other words bin folder.

The file should be located in src/main/resources when loading using Class loader

In short, you have to use .getClass().getClassLoader().getResource(fileName) to load the file in your case.

like image 77
Rahul Avatar answered Oct 06 '22 06:10

Rahul