Workaround for use of Getservletcontext () in servlet to get ServletContext object java.lang.NullPointerException (null pointer) exception
Today encountered a java.lang.NullPointerException (null pointer) exception in the Servlet service method that gets the ServletContext object, the code is as follows:
gets the ServletContext object this.getservletcontext ();
This is a very strange and first encounter, because it was written in the Doget/dopost method of the servlet, and did not appear in the previous ServletContext object java.lang.NullPointerException (null pointer) exception, the internet looked up the reason for this exception: it was I rewrote the init (ServletConfig) method, but the overridden Init method does not call Super.init (config); that's what caused the error! The parent class's Init (ServletConfig) has a reference to handle getting the ServletContext object, which can be passed Getservletcontext () in the Doget/dopost/service method method. Method gets the Severletcontext object! after overriding the Servlet's Init method, be sure to remember to call the parent class's Init method, otherwise use the Service/doget/dopost method The java.lang.NullPointerException exception occurs when the Getservletcontext () method gets the ServletContext object
Throws servletexception{// rewrite the servlet's Init method and remember to call the parent class's Init method, otherwise the service/doget/ The Dopost method uses the Getservletcontext () method to get the ServletContext object when the java.lang.NullPointerException exception 3 super is present . Init (config); 4}
Workaround for use of Getservletcontext () in servlet to get ServletContext object java.lang.NullPointerException (null pointer) exception