JSP <input type= "checkbox" > Tagged null pointer exception

Source: Internet
Author: User
Tags try catch

Today I see a piece of code in a book like this:

This is the code for the <input type= "checkbox" > tag

<BR>Choose the singer you like:<inputtype= "checkbox"name= "Singer"value= "Zhang">singer Zhang<inputtype= "checkbox"name= "Singer"value= "Lee">Lee singer<inputtype= "checkbox"name= "Singer"value= "King">Wang singer


The following is an array implementation

String personname[]=request.getparametervalues ("singer");//Gets the name of the singer, using an array to implement  if (Personname==null)     {        out.println ("One does not like");            }     else     {for           (int i=0;i<=personname. length;i++)       {          out.println ("<br > You like the singer "+personname[i");       }       }     

The result throws a null pointer exception, in the array where the contents of the previous selection box are assigned to the personname[] array, and then not every selection box is selected, this is a multi-box, optional, not selected, did not assign the array, so thrown a null pointer exception. That's how I solved it:

  if (personname==null)     {        out.println ("One does not like");            }     else     {     try{  //Why add a Try Catch statement without an error  without adding a system report null pointer exception for        (int i=0;i<= PersonName. length;i++)       {          out.println ("<br> your favorite singer" +personname[ I]);       }       }       catch (Exception e)       {         e.printstacktrace ();       }     }          Out.println ("<br> This is the hidden message you submitted" +secretmsg);


Catch the exception with a try Catch statement, and the result runs successfully! Although this is a very small problem, but I found a problem, these textbooks are not entirely correct, the true truth is in practice. More hands-on, more practice, more thinking ...

JSP <input type= "checkbox" > Tagged null pointer 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.