Solved

Loading properties file from Servlet

Posted on 2006-10-29
4
427 Views
Last Modified: 2013-11-24
Trying to load a properties file from a Servlet and I'm getting null pointer excepion.  I can't figure out

1.  The code

// servet stuff...
Properties myProps = new Properties();
myProps.load(getClass().getResourceAsStream("conf/build.properties");  // << -- thrown from here **
String foo = (String) myProps.getProperty("foo);

2. On my appserver the properties file lives here.
 
$APPDIR/WEB-INF/classes/conf/build.properties

3.  Exception

java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:365)
        at java.util.Properties.load(Properties.java:293)
        at com.saferX.util.misc.PropertiesUtil.init(PropertiesUtil.java:71)  << -- ** Line marked above.
        at com.saferX.util.misc.PropertiesUtil.<init>(PropertiesUtil.java:43)
        at com.saferX.util.misc.PropertiesUtil.getPropertiesUtil(PropertiesUtil.
java:103)
        at com.saferX.web.action.LoginAction.initVersionInfo(LoginAction.java:20
1)
        at com.saferX.web.action.LoginAction.executeLogin(LoginAction.java:157)
        at com.saferX.web.action.LoginAction.execute(LoginAction.java:52)
        at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:421)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:226)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:116
4)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:154)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:92)
        at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterC
hain.java:106)
        at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain
.java:173)
        at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocatio
n.java:229)
        at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274
)
        at com.caucho.server.port.TcpConnection.run(TcpConnection.java:511)
        at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:516)
        at com.caucho.util.ThreadPool.run(ThreadPool.java:442)
        at java.lang.Thread.run(Thread.java:595)
java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:365)
        at java.util.Properties.load(Properties.java:293)
        at com.saferX.util.misc.PropertiesUtil.init(PropertiesUtil.java:59)
        at com.saferX.util.misc.PropertiesUtil.getProperty(PropertiesUtil.java:1
31)
        at com.saferX.web.action.LoginAction.initVersionInfo(LoginAction.java:20
5)
        at com.saferX.web.action.LoginAction.executeLogin(LoginAction.java:157)
        at com.saferX.web.action.LoginAction.execute(LoginAction.java:52)
        at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:421)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:226)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:116
4)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:154)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:92)
        at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterC
hain.java:106)
        at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain
.java:173)
        at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocatio
n.java:229)
        at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274
)
        at com.caucho.server.port.TcpConnection.run(TcpConnection.java:511)
        at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:516)
        at com.caucho.util.ThreadPool.run(ThreadPool.java:442)
        at java.lang.Thread.run(Thread.java:595)

------------------------------------------------------------------------
How do I fix it and read property 'foo' from that properties file?
0
Comment
Question by:tmonteit
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17829623
Try

myProps.load(getClass().getResourceAsStream("/conf/build.properties");
0
 

Author Comment

by:tmonteit
ID: 17829842

I'm confused What's different than what I'm already trying?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17829886
You have no opening forward slash
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17830317
:-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 24
even odd program using while loop 3 41
javap not working 8 46
Problem to Alipay 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question