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.

inside Controller.java

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"));


EmployeeDAO.addTrips(id,tripsInitialvalue);


doEmployees(request, response);

}


inside my EmployeeDAO.java

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


and my employees.jsp file:

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


error log:



Etat HTTP 500 - null
message null
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.valueOf(Unknown Source)
projet.controleur.Controleur.doUpdateEmployee(Controller.java:177)
projet.controleur.Controleur.doGet(Controller.java:109)
projet.controleur.Controleur.doPost(Controller.java:69)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Answer Source

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

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

int id = Integer.valueOf(request.getParameter("id"));
EmployeeDAO.addTrips(id,tripsInitialvalue);

} 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