user2065929 user2065929 - 3 months ago 35
Java Question

getClassLoader().getResourceAsStream("../resources/config.properties") returns null

Hi all i am getting the error

Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at richard.fileupload.FileUploadController.loadProp(FileUploadController.java:48)
... 60 more


whenever my properties class is called, this is the code

private Properties configProp = new Properties();

@PostConstruct
public void loadProp() {
System.out.println("Loading properties");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
try {
configProp.load(in);
System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList"));
} catch (IOException e) {
e.printStackTrace();
}
}
private String destinationPDF = configProp.getProperty("destinationPDF");
public String destination = configProp.getProperty("destination");
private String username;


the error seems to be coming from this line :

InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
try {
configProp.load(in);


what is causing this error and how can i solve it ?

the code above, should point to the properties file and then retrive the three variables values from it, it is not getting as far as the

System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList


before i get the error

this is my directory structure

enter image description here

as you can see the properties file is in resources/, how would i get a link to this

EDIT :

ok so it works perfectly fine with the full link to the file :

configProp.load(new FileInputStream("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/WEB-INF/config.properties"));


but no matter where i put the file in the projet i can not get it to load at all, why is this ?

Answer

This line doesn't make sense:

this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties")

ClassLoader.getResourceAsStream() loads a resource from the classpath, using a package path starting at the root package. And there is nothing above the root package.