1

i am getting the java.lang.NullPointerException error when i run my code. I don't know where the problem is coming from

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/WEB-INF/views/home.jsp");
    req.setAttribute("restName", AppConfigPropertyReader.getProperty("RESTAURANT_NAME"));
    req.setAttribute("restDesc", AppConfigPropertyReader.getProperty("RESTAURANT_DESC"));
    req.setAttribute("contact", AppConfigPropertyReader.getProperty("RESTAURANT_CONTACT"));


    dispatcher.forward(req, resp);
}

and also at

public static String getProperty(String key) {
    String value = null;
    InputStream input;
    try {
        input = AppConfigPropertyReader.class.getClassLoader().getResourceAsStream(PROPERTY_FILE);
        Properties p = new Properties();
        p.load(input);
        value = p.getProperty(key);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return value;
}

the error am getting is

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 com.restaurant.util.AppConfigPropertyReader.getProperty(AppConfigPropertyReader.java:18)*
*at com.restaurant.controller.HomeController.doGet(HomeController.java:23)*
at javax.servlet.http.HttpServlet.service(HttpServlet.java:686)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user1922
  • 11
  • 1
  • Is this happening rarely or every time? I ask because most `HttpServlet` implementations will be significantly multithreaded when under heavy load and there *may* be an issue with `getResourceAsStream` being called in parallel. A good test of this might be to make `getProperty` synchronized and see if the issue goes away. – OldCurmudgeon Jul 11 '18 at 08:14
  • 1
    This suggests that `input` is null, which means that the properties-file was not found on the class path. – Mark Rotteveel Jul 11 '18 at 08:20
  • it happens everytime – user1922 Jul 11 '18 at 09:14

0 Answers0