Javaweb Learning (5.5)---use Getservletcontext () in servlet to get a solution for java.lang.NullPointerException (null pointer) exception in ServletContext object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.