getServletConfig().getInitParameter("count1") java.lang.NullPointerException

來源:互聯網
上載者:User

標籤:執行   his   https   樣書   而且   沒有   gen   pos   內容   

通常在doget中

System.out.println(getServletConfig());            System.out.println(getServletConfig().getInitParameter("count1"));

這樣書寫時完全沒有問題的,但是由於重寫了init()函數,這時就發生而來異常。

 

 

先看一下init()的定義,init()是在GenericServlet中實現的【由可以看出,GenericServlet是httpServlet的源碼】,下面是源碼,

public void init(ServletConfig config) throws ServletException {    this.config = config;    this.init();    }

 可以看出在init中給全域的config進行了賦值,所以通常我們在執行doget()時才可以使用config對象

所以:

在我們重寫init方法是一定要記得調用父類的init方法
@Overridepublic void init() throws ServletException {    super.init();    /*     * 自訂內容     */}

因此,我的程式會報錯也就不難理解了,在重寫init時並沒有調用父類的init(),而且也沒有對config對象進行賦值,所以在doget擷取的時候,只能調用到一個空的對象,在調用他的字方法時,由於它是Null 物件,沒有被執行個體化,自然就會null 指標異常。

 

getServletConfig().getInitParameter("count1") java.lang.NullPointerException

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.