0

I have problem with my context-params in my web application. My methodes

 getServletContext().getInitParameter("pass");

Are returning null. Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Log4jWebDemo1</display-name>
<context-param>
    <param-name>log4j-config-location</param-name>
    <param-value>WEB-INF/log4j.properties</param-value>
</context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>dbc:postgresql://localhost/OlapTest</param-value>
    </context-param>
    <context-param>
        <param-name>name</param-name>
        <param-value>org.postgresql.Driver</param-value>
    </context-param>
    <context-param>
        <param-name>usr</param-name>
        <param-value>postgres</param-value>
    </context-param>
    <context-param>
        <param-name>pass</param-name>
        <param-value>admin</param-value>
    </context-param>
  <servlet>
    <servlet-name>Hello World</servlet-name>
    <servlet-class>pl.javastart.servlets.HelloWorldServlet</servlet-class>

  </servlet>
  <servlet-mapping>
    <servlet-name>Hello World</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>

And my servlet:

public class HelloWorldServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;
       private String url2 = ""; 
       private String driver = ""; 
       private String username = ""; 
       private String password = ""; 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        PrintWriter out = response.getWriter();
        url2 = getServletContext().getInitParameter("url");
        driver = getServletContext().getInitParameter("name");
        username = getServletContext().getInitParameter("usr");
        password = getServletContext().getInitParameter("pass");

        DatabaseHandling d1 = new DatabaseHandling(driver,url2,username,password);

        d1.createConnection(out);

        }
}

After executing my application it throws

java.lang.NullPointerException

15
  • 1
    Can you paste the full exception stack trace ? Commented Sep 30, 2016 at 10:25
  • Already did it my friend Commented Sep 30, 2016 at 10:34
  • can you show DatabaseHandling class code Commented Sep 30, 2016 at 10:37
  • Its redundant I think, when I manualy put string values where I am executing getServletContext().getInitParameter(); The app works fine Commented Sep 30, 2016 at 10:39
  • before calling DatabaseHandling d1 = new DatabaseHandling(driver,url2,username,password); this line just print all string values. Commented Sep 30, 2016 at 10:40

1 Answer 1

1

There can be few possibilities that leads to the NullPointerException exception

  1. PostgresSQl driver(jar file) library is not added in your classpath.
  2. You need to share your DatabaseHandling class so as to check the code written for connecting with DB.
  3. change the web.xml as below:

    <context-param> <param-name>url</param-name> <param-value>jdbc:postgresql://localhost/OlapTest</param-value> </context-param>

J is missing. Check the error it says dbc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.