54114

getClassLoader().getResourceAsStream(“../resources/config.properties”) returns null [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> Where to place and how to read configuration resource files in servlet based application? <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

    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

    <img src="https://i.stack.imgur.com/yHpiS.png" alt="enter image description here">

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

    <strong>EDIT :</strong>

    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 ?

    Answer1:

    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.

Recommend

  • Spring - Integration test of MultipartFileUpload with container and custom validator
  • How to register many object as beans in Spring Java Config?
  • MVC 4 Beta with Mobile Project FIle Upload does not work
  • Glassfish denies access to JSF page, returning 403 HTTP response code
  • JSF - Keep Faces Messages after redirect from @PostConstruct
  • Iterate within
  • Will a method annotated with @PostConstruct be guaranteed to execute prior to a method with @Schedul
  • Refering to the class itself from within a class mehod in Objective C
  • Google Custom Search with transparent background
  • Java: can you cast Class into a specific interface?
  • FileReader+canvas image loading problem
  • Using jQuery closest() method with class selector
  • Insert into database using onclick function
  • Can I display google adwords (AdView) in javafx on android
  • Align navbar back button on right side
  • How to add date and time under each post in guestbook in google app engine
  • How to set/get protobuf's extension field in Go?
  • output of program is not same as passed argument
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Web-crawler for facebook in python
  • Why winpcap requires both .lib and .dll to run?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?