This article is organized from the Lonely Wolf Big Blog,,,
Getting the ServletContext object in the Servlet's service method also java.lang.NullPointerException (null pointer) exception, the code is as follows:
gets the ServletContext object this.getservletcontext ();
It turns out that the init (ServletConfig) method was rewritten, but the overridden Init method did 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 the Getservletcontext () method is used in the Service/doget/dopost method to get the ServletContext object. Java.lang.NullPointerException exception
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}
Javaweb Learning (5.5)---Get ServletContext object in servlet using Getservletcontext () java.lang.NullPointerException (null pointer) Workaround for exception