0

So, im doing a simple practice using Log4J in a web app. Here is my code:

package tot;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * Servlet implementation class pro
 */
@WebServlet("/pro")
public class pro extends HttpServlet {
private static final long serialVersionUID = 1L;

private Logger logg = null;

public void inut() {
    logg = Logger.getRootLogger();
    BasicConfigurator.configure();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    logg.setLevel(Level.FATAL);
    logg.debug("debug");
    logg.info("info");
    logg.warn("warn");
    logg.error("error");
    logg.fatal("fatal");


    response.setContentType("text/html");
    PrintWriter printwriter = response.getWriter();
    printwriter.println("<html>");
    printwriter.println("<head><title>log4j</title></head>");
    printwriter.println("<body>");
    printwriter.println("no config <br>");
    printwriter.println("logger name: " + logg.getName());
    printwriter.println("</body>");
    printwriter.println("</html>");
}

}

i created a dynamic web project and im using apache tomcat 9.0.6 runtime. i also created a servlet. when i run my code i get the following error message:

HTTP Status 500 – Internal Server Error


Type Exception Report

  Description The server encountered an unexpected condition that prevented it 
from fulfilling the request.

Exception
java.lang.NullPointerException
    tot.pro.doGet(pro.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.


Apache Tomcat/9.0.6

Additionally, the following error message prints to the console:

java.lang.NullPointerException
    at tot.pro.doGet(pro.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

what im expecting to happen is the http page to display the title and body message; and the logger message "fatal" should print to the console. From what ive researched, the solutions i found were to clear the cache, web browsing history, and refresh the page (which ive done). i feel the error is generating from how i set up the server but i cannot find what i did wrong.

Any help is highly appreciated.

Source Code
  • 15
  • 1
  • 4

1 Answers1

0

The internal Server error is caused by a null pointer exception that occurs when you are trying to access the properties of an object which has not been initialized yet.

In this code the logger has not been initialized yet:

logg.setLevel(Level.FATAL);

The inut method is not called yet meaning the logger is not initialized leading to the value of logg being NULL. This results in a null pointer exception. In order to resolve the issue you need to invoke the inut() method or we can create a static variable:

final static Logger logger = Logger.getLogger(classname.class);

For more information about the static variables take a look at What does the 'static' keyword do in a class?

Anoop saju
  • 480
  • 3
  • 17
  • ah yes it worked. however im now getting an error in the console that states "Please initialize the log4j system properly". It seems this is generated because i did not provide an appender (which is stated in the console as well). please bare in mind that im am very new to log4j but it was my understanding that the statement "BasicConfigurator.configure();" allows you to print different log messages without the use of an appender/property statement. so my question would be, can you print a log message without the use of an appender? and if so what is the proper format? – Source Code Apr 02 '18 at 04:37