Getservletconfig (). Getinitparameter ("Count1") java.lang.NullPointerException

Source: Internet
Author: User

Usually in Doget.

System.out.println (Getservletconfig ());            System.out.println (Getservletconfig (). Getinitparameter("Count1"));

There is absolutely no problem in writing this, but because the init () function is rewritten, this happens unexpectedly.

First look at the definition of init (), Init () is implemented in the Genericservlet "can be seen, Genericservlet is HttpServlet source", the following is the source,

 Public void throws servletexception {    this. config = config;       This . Init ();    }

We can see that the Global config is assigned in init, so we usually use the Config object when we execute Doget ().

So:

before we rewrite the Init method, be sure to remember to call the parent class's Init method
@Override  Public void throws servletexception {    super. Init ();     /*      * Customize content     * / }

Therefore, my program will not be difficult to understand the error, in the rewrite Init did not call the parent class init (), and also did not assign a value to the Config object, so when Doget gets, can only call to an empty object, when calling his word method, because it is an empty object, is not instantiated , you will naturally have null pointer exceptions.

Getservletconfig (). Getinitparameter ("Count1") java.lang.NullPointerException

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.