2

I have a dropdown menu which pulls data from a SQL table. Everything works fine and I'm able to select the value and submit the form. However, my scriptlet gives NULL exception when I convert the value from String. What am I doing wrong?

testCars.jsp

<form method=post action=“testCars.jsp">
<div class="cell">
        Select CarID
        <div class="input-control select mutiple full-size">
            <select id=“carID” name=“carID” onchange="listCarIDFromDatabase();”>
            <option selected disabled>--SELECT--</option>
            <%
            ArrayList<Integer> result = carDAO.getCarID(Integer.parseInt(id));
            for (int r: result){
                out.println("<option value='" + r + "'>" + r + "</option>");
                  request.setAttribute("r", r);
            }
            %>
            </select>

        </div>
    </div>

The input returncarID is populated based on AJAX success onchange="listCarIDFromDatabase();

   <button class="button primary" type="submit">Generate</button></form>
   <input name=“returncarID” id=“returncarID”>   


<%
//Scriptlet gets the value of the dropdown in next line, but when I convert to Int it is NULL
String y = request.getParameter(“returncarID”);
out.println(y);  <——Prints out value here 

int x = Integer.parseInt(y);
out.println(x); <———Null Exception here


%>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
raptor496
  • 237
  • 1
  • 3
  • 12

1 Answers1

1

If the output is 900056 and you are getting a java.lang.NumberFormatException when you write :

int x = Integer.parseInt(y);
out.println(x);

I think the problem is with the intial String it may have a trailing or leading whitespace, to avoid this problem use .trim() with your String before parsing it:

int x = Integer.parseInt(y.trim());
out.println(x);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78