1

I'm making a select box with dates so I created a loop for the options like this:

<select name="BDay" /> 
<%
    for (int i = 1; i <= 31; i++) {
        out.print("<option value=" + i + ">");
        out.print(i);
        out.print("</option>");
    } %> 
        </select>
         <select name="BMonth" /> 
    <%
        for (int j = 1; j <= 12; j++) {
            out.print("<option value=" + j + ">");
            out.print(j);
            out.print("</option>");
        }
     %> 
    </select> 
    <select name="BYear" /> 
    <%
        for (int k = 1915; k <= 2011; k++) {
            out.print("<option value=" + k + ">");
            out.print(k);
            out.print("</option>");
        }
     %> 
    </select>

and then in a different page when I try to get the information the user picked (like this):

int BDay = Integer.parseInt(request.getParameter("BDay"));
int BMonth = Integer.parseInt(request.getParameter("BMonth"));
int BYear = Integer.parseInt(request.getParameter("BYear"));

I get this error: java.lang.NumberFormatException: null

Can someone please help? Thanks in advance!

Roman C
  • 47,329
  • 33
  • 60
  • 147
  • Your `select` tags are self closing (they end with a trailing slash, eg ``) therefore your HTML is invalid and the select elements are closed before the options are printed. Presumably when you submit the form the parameters are empty or null thus trying to parse them as an int fails. – Moob Jan 06 '16 at 11:27

1 Answers1

0

Without self-closing tags the code will look like

<select name="BDay" > 
<%
    for (int i = 1; i <= 31; i++) {
        out.print("<option value=" + i + ">");
        out.print(i);
        out.print("</option>");
    } %> 
</select>
<select name="BMonth" > 
<%
    for (int j = 1; j <= 12; j++) {
        out.print("<option value=" + j + ">");
        out.print(j);
        out.print("</option>");
    }
 %> 
</select> 
<select name="BYear" > 
<%
    for (int k = 1915; k <= 2011; k++) {
        out.print("<option value=" + k + ">");
        out.print(k);
        out.print("</option>");
    }
 %> 
</select>

Read more about serf-closing tags Are self-closing tags valid in HTML5?

Community
  • 1
  • 1
Roman C
  • 47,329
  • 33
  • 60
  • 147
  • It takes a little investigation to understand what your solution is. You might have made it clear to readers that the OP had self-closing `select` tags and thus invalid HTML. I for one had look hard to see the difference between your posted code and the OP's. – Moob Jan 06 '16 at 11:19
  • @Moob First time I thought it was a typo putting serf-closed tags to OP's code, but doing it three times is already a bug. Also it produces invalid html, that's why I included the link to the page it OP needs to know which parts of the code are valid and how to use self-closing tags if needed. In the current context it's clear that self-closing tags invalidate the output html even if the tags are valid against htm5 standards. – Roman C Jan 06 '16 at 11:30