I can not update a field in a Java-Web form

0

At the moment I want to modify a field in the form of a record, in the case of the Age field, I modify it to 19 but at the time of modifying it I get the following error:

 GRAVE: Servlet.service() para servlet MisServlets.ServletPaciente lanzó excepción
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at MisServlets.ServletPaciente.actualizar(ServletPaciente.java:98)
    at MisServlets.ServletPaciente.service(ServletPaciente.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

This is the code of the ServletPaciente (Update):

private void actualizar(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PacienteDTO p = new PacienteDTO();
        String cod = request.getParameter("txt_CodPaciente");
        String nom = request.getParameter("txtNombre");
        String ape = request.getParameter("txtApellidos");
        String dni = request.getParameter("txtDni");
        String fechaNacimiento = request.getParameter("txtFechaNacimiento");
        String edad = request.getParameter("txtEdad");
        String sexo = request.getParameter("cbo_sexo");
        String dir = request.getParameter("txtDireccion");
        String correo = request.getParameter("txtCorreo");
        String telefono = request.getParameter("txtTelefono");
        String fechaRegistro = request.getParameter("txtFechaRegistro");

        p.setNombre(nom);
        p.setApellidos(ape);
        p.setDni(dni);
        p.setFechaNacimiento(fechaNacimiento);
        p.setEdad(Integer.parseInt(edad));
        SexoDTO s = new SexoDTO();
        s.setCodSexo(Integer.parseInt(sexo));
        p.setSexo(s);
        p.setDireccion(dir);
        p.setCorreo(correo);
        p.setTelefono(telefono);
        p.setFechaRegistro(fechaRegistro);
        p.setCodPaciente(Integer.parseInt(cod));
        int estado = pacienteService.actualizarPaciente(p);
        if (estado != -1)
            listar(request,response);
    }

This is the code that sent the sentence to the Database to update:

public int actualizarPaciente(PacienteDTO obj) {
        int estado = -1;
        Connection cn = null;
        PreparedStatement pstm = null;
        try {
            cn = MySqlDBConexion.getConexion();
            String sql = "update paciente "
                    + " set nombre=?, apellidos=?, dni=?, fecha_nacimiento=?, edad=?, codsexo=?, direccion=?, correo=?, "
                    + " telefono=?, fecha_registro=? "
                    + " where codpaciente=?";
            pstm = cn.prepareStatement(sql);
            pstm.setString(1, obj.getNombre());
            pstm.setString(2, obj.getApellidos());
            pstm.setString(3, obj.getDni());
            pstm.setString(4, obj.getFechaNacimiento());
            pstm.setInt(5, obj.getEdad());
            pstm.setInt(6, obj.getSexo().getCodSexo());
            pstm.setString(7, obj.getDireccion());
            pstm.setString(8, obj.getCorreo());
            pstm.setString(9, obj.getTelefono());
            pstm.setString(10,obj.getFechaRegistro());
            pstm.setInt(11, obj.getCodPaciente());
            estado = pstm.executeUpdate();
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                if (pstm != null)
                    pstm.close();
                if (cn != null)
                    cn.close();
            } 
            catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return estado;
    }

Updating Patient Form Code:

<body>

    <%
        PacienteDTO p = (PacienteDTO) request.getAttribute("pasar");
    %>

<jsp:include page="MenuAdministrador.jsp"></jsp:include><br>
<div class="container">
    <br><br>
    <form class="form-horizontal" action="ServletPaciente?tipo=actualizar" name="frmactualizar" method="post">
        <fieldset>
            <legend style="text-align:center"><label>MANTENIMIENTO DEL PACIENTE</label></legend>
            <fieldset>
                <legend><label>Datos del Paciente</label></legend>
                    <div class="form-group">
                        <label class="control-label col-md-2">Código:</label>
                        <div class="col-md-1">
                            <input type="text" class="form-control" disabled value=<%=p.getCodPaciente()%> name="txt_CodPaciente">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Nombre:</label>
                        <div class="col-md-3">
                            <input type="text" class="form-control" value="<%=p.getNombre()%>" name="txtNombre">
                        </div>
                        <label class="control-label col-md-3">Apellidos:</label>
                        <div class="col-md-4">
                            <input type="text" class="form-control" value="<%=p.getApellidos()%>" name="txtApellidos">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">DNI:</label>
                        <div class="col-md-3">
                            <input type="text" class="form-control" value=<%=p.getDni()%> name="txtDni">
                        </div>
                        <label class="control-label col-md-3">Fecha de Nacimiento:</label>
                        <div class="col-md-3">
                            <input type="text" class="form-control" value=<%=p.getFechaNacimiento()%> name="txtFechaNacimiento">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Dirección:</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" value="<%=p.getDireccion()%>" name="txtDireccion">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Teléfono:</label>
                        <div class="col-md-3">
                            <input type="text" class="form-control" value=<%=p.getTelefono()%> name="txtTelefono">
                        </div>
                        <label class="control-label col-md-3">Sexo:</label>
                        <div class="col-md-3">
                            <select name="cbo_sexo" class="form-control">
                                <%
                                    String sexo[]={"","Masculino","Femenino"};
                                    String estado="";
                                    for(int i=1;i<sexo.length;i++){
                                        if(p.getSexo().getCodSexo()==i){
                                            estado="selected";
                                        }
                                        else
                                            estado="";
                                %>
                                <option value="<%=i%>" <%=estado%>><%=sexo[i]%></option>
                                <%
                                    }
                                %>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Edad:</label>
                        <div class="col-md-1">
                            <input type="text" class="form-control" value=<%=p.getEdad()%> name="txtEdad">
                        </div>
                        <label class="control-label col-md-5">Fecha de Registro:</label>
                        <div class="col-md-3">
                            <input type="text" class="form-control" disabled value=<%=p.getFechaRegistro()%> name="txtFechaRegistro">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Correo:</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" value=<%=p.getCorreo()%> name="txtCorreo">
                        </div>
                    </div>

                    <div class="form-group">
                    <table align="center">
                        <tr height="30px">
                            <td colspan="2" align="center">
                                <a href="ServletPaciente?tipo=listar" target="_self" style="text-align:center">BUSCAR PACIENTE</a>
                            </td>
                        </tr> 
                    </table>
                    </div>

            </fieldset>
            <table align="center" style="border-collapse:separate; border-spacing: 20px">
                <tr>
                    <td><input type="button" name="btnRegistrar" value="REGISTRAR" class="btn btn-md btn-primary" disabled/></td>
                    <td><input type="submit" name="btnModificar" value="MODIFICAR" class="btn btn-md btn-primary"/></td>
                    <td><a href="Paciente.jsp"><input type="button" name="btnNuevo" value="NUEVO" class="btn btn-md btn-primary"/></a></td>
                    <td><input type="button" name="btnCancelar" value="CANCELAR" class="btn btn-md btn-primary"/></td>
                    <td><input type="button" name="btnRegresar" value="REGRESAR" class="btn btn-md btn-primary"/></td>
                </tr>
            </table>
        </fieldset>
    </form>
</div>
</body>
    
asked by Bruno 23.11.2016 в 16:11
source

2 answers

2

Verify that all the data that you send from the form is of the type that they receive. Verify that by no means the data goes null also.

    
answered by 23.11.2016 / 17:03
source
2

This is possibly because the String that you are trying to read is not Integer , I see that besides Age, you also use it in Sex and codPaciente. There are times when fields also bring blank spaces. Make sure you do not bring blank spaces and make it Integer .

The exception suggests that the field is not a number, NumberFormatException . For Integer.parseInt () expect the argument to be a Integer number.

The Servlet is dialing you on line 98 ServletPaciente.java:98 , go to that line and see what field you are setting and print it before calling the function.

    
answered by 23.11.2016 в 16:55