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