2

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 ?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user2065929
  • 999
  • 4
  • 19
  • 33
  • Where is your config.properties file located in classpath – jmj Feb 16 '13 at 17:40
  • it doesn't look like its a maven project, move resources to WEB-INF/resources and then access it by this.getClass().getClassLoader().getResourceAsStream("resources/config.properties") – jmj Feb 16 '13 at 17:51
  • it is a netbeans JSF project using a glassfish server, i have tried your suggestion but same error, i have now copied it to the root of my src folder, i access it using InputStream in = this.getClass().getClassLoader().getResourceAsStream("/config.properties"); but am still getting the same error – user2065929 Feb 16 '13 at 18:06
  • 1
    Just to point it out. You can make @Jigar Joshi suggestion work with `getServletContext().getResourceAsStream("/WEB-INF/resources/config.propertie‌​s");` but I don't think it is good practice (although a lot of Web Frameworks (ab)use WEB-INF for configuration purposes). This kind of configuration file should be in `WEB-INF/classes` where it belongs. – Anthony Accioly Feb 16 '13 at 18:19

1 Answers1

3

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.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
  • added a print screen of my directory, it is a netbeans JSF project using a glassfish server, i have tried `getResourceAsStream("/config.properties")` but still get the same error :( – user2065929 Feb 16 '13 at 17:48
  • Is the config.properties located in WEB-INF/classes, or in any of the jars in WEB-INF/lib in the generated war file? If it isn't, there's no way to load it using the class loader, because the classpath of a webapp is constituted by WEB-INF/classes and by all the jars in WEB-INF/lib, and the class loader uses the classpath to load resources. – JB Nizet Feb 16 '13 at 17:51
  • ok thanks, so if i move this file into WEB-INF i should be ok ? what link address would i need to provide then – user2065929 Feb 16 '13 at 17:52
  • 2
    Just copy `config.properties` to the root of your `src` folder. It will be copied to the right place to be accessed with `getResourceAsStream("/config.properties")` – Anthony Accioly Feb 16 '13 at 17:53
  • 1
    Not to WEB-INF, no. To a place which will make it appear in `WEB-INF/classes` in the generated webapp: the `src` folder. – JB Nizet Feb 16 '13 at 17:55
  • i have copied it to the root of my src folder, i access it using `InputStream in = this.getClass().getClassLoader().getResourceAsStream("/config.properties");` but am still getting the same error – user2065929 Feb 16 '13 at 17:59
  • 1
    Have you rebuild and redeployed the war / exploded archive? Have your config.properties file been copied by the IDE to `WEB-INF/classes`? – Anthony Accioly Feb 16 '13 at 18:08
  • i have rebuild and redeployed, the config.properties was moved to src root folder, i do not have `WEB-INF/classes` in my project – user2065929 Feb 16 '13 at 18:12
  • 1
    Click the button `Clean and Build`. Inside your Netbeans project look for a `dist` folder. Open the war file with your favorite zip tool and tell us if you see a `WEB-INF/classes/config.properties` file inside it. If you do, simple load it either with `this.getClass().getResourceAsStream("/config.properties")` or `this.getClass().getClassLoader().getResourceAsStream("config.properties");` (notice the missing slash) – Anthony Accioly Feb 16 '13 at 18:31
  • Thank you i have tried that and i do not see a `dist` folder, but what i have noticed is 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 ? – user2065929 Feb 16 '13 at 18:45
  • 2
    Yes you do have a dist folder (Look in Windows Explorer: `D:\Documents\NetBeansProjects\printing~subversion\fileupload` after a `Clean and Build`). Hard coding the absolute path will not work when you deploy your war file in a different server. Just put the `config.properties` inside `src` as suggested, take a look at the generated war and use one of the two loading methods that I mentioned in my previous comment. – Anthony Accioly Feb 16 '13 at 18:51
  • ok i have found the `dist` folder, and inside classes i can not see the config.properties file am trying it again now – user2065929 Feb 16 '13 at 18:53
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24636/discussion-between-anthony-accioly-and-user2065929) – Anthony Accioly Feb 16 '13 at 18:54