• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

loading a properties file

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a properties file in the same folder as my java class. I have tried the following code to load it, but it fails. I get an uncaught NullPointer Exception.

main.java
--------------------------------------------------------------------------------------------------
Properties properties = new Properties();
InputStream stream = null;
try {
stream = getClass().getResourceAsStream("/test.properties");
properties.load(stream);
} catch (IOException e) {
}

and my test.properties file is as follows
---------------------------------------------------------------------------------------------------
subject=Test Mail
body=Dear ${0}:\n\nThis is a test mail.

and the code using message formatter to substitute the values is as follows :

main.java
--------------------------------------------------------------------------------------------------------
String temp=properties.getProperty("body");
java.text.MessageFormat formatter = new java.text.MessageFormat("");
formatter.applyPattern(temp);

Object[] messageArguments = {"Jim Berkley"};

String output = formatter.format(messageArguments);


for some reason, this does not work.... Any help is appreciated!!

TIA
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Class.getResourceAsStream javadoc

...an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').



As your properties file is in the same directory as your class file, you need it to be prefixed by the modified package name, so you need to remove the '/' at the start
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic