someangular someangular - 4 years ago 188
Java Question

java jsp file update table field in database

I am trying to update the number of trips done by employees.
In the table below, I get the list of the employees and the number of trips done by each, and an input field to update that number. However, I get

error 500 null

Could somebody help me to resolve this issue? thanks a lot.


public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {


} else if (methode.equals("post") && action.equals("/updateemployees")) {
doUpdateEmployee(request, response);

private void doUpdateEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int tripsInitialvalue = Integer.valueOf(request.getParameter("nbTrips"));
[line: 177] int id = Integer.valueOf(request.getParameter("id"));


doEmployees(request, response);


inside my

public static Employee addTrips(int id, int tripsToAdd) {
EntityManager em = GestionFactory.factory.createEntityManager();
Employee employee = em.find(Employee.class, id);
int tripsInitialvalue = employee.getNbTrips();
employee.setNbTrips(tripsInitialvalue + tripsToAdd);
return employee;

and my employees.jsp file:

<form method="post" action="<%= getServletContext().getContextPath()%>/do/updateemployees">
<th>Employee ID</th>
<th>Number of Trips</th>
<th>Edit Trips</th>
<% for (Employee employee : employees) {%>
<td><input type="number" name="nbTrips" size="2" value="<%=employee.getNbTrips()%>"></td>
<% } %>
<input type="submit" value="Update"/>

error log:

Etat HTTP 500 - null
message null
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.valueOf(Unknown Source)

Answer Source

so from the error details it is apparent that the valueOf is trying to parse null in the cannot parse a "null" to int.

if(request.getParameter("id") != null) {

int id = Integer.valueOf(request.getParameter("id"));

} else {
  //decide what to do when id is null

EDIT : To get values of all textboxes use request.getParameterValues('nbTrips'), this will return all the values for elements with same 'nbTrips'

String[] nbTrips = request.getPrameterValues('nbTrips')

will return all the text box values with name 'nbTrips' into String array nbTrips which you can iterate and get each values from textBox.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download