Null Pointer Exception when trying to perform a @PUT

Asked

Viewed 48 times

-1

I’m testing the put by Postman but is giving null Pointer Exception when I try to perform @put


    @PUT
    @Path("/{idCliente}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response atualizarCliente(@PathParam("idCliente") int id, Cliente cliente) throws Exception {
        Cliente atualizarCliente = clienteDao.encontrarPorId(cliente.getIdCliente());
        System.out.println(cliente.toString());

        atualizarCliente.setIdCliente(cliente.getIdCliente());
        atualizarCliente.setNome(cliente.getNome());
        atualizarCliente.setCpf(cliente.getCpf());
        atualizarCliente.setEmail(cliente.getEmail());
        atualizarCliente.setDataDeNascimento(cliente.getDataDeNascimento());

        clienteDao.atualizar(atualizarCliente);

        return Response.ok().build();
    }
public boolean atualizar(Cliente cliente) throws Exception {
        boolean sucessoUpdate = false;

        String sql = " UPDATE CLIENTE SET NOME = ? , CPF = ? , EMAIL = ? , DATA_NASCIMENTO = ? WHERE IDCLIENTE = "
                + cliente.getIdCliente();

        Connection conexao = Banco.getConnection();
        PreparedStatement prepStmt = Banco.getPreparedStatement(conexao, sql);

        try {
            Date data = new SimpleDateFormat("yyyy-MM-dd").parse(cliente.getDataDeNascimento());

            prepStmt.setString(1, cliente.getNome());
            prepStmt.setString(2, cliente.getCpf());
            prepStmt.setString(3, cliente.getEmail());
            prepStmt.setDate(4, new java.sql.Date(data.getTime()));

            int codigoRetorno = prepStmt.executeUpdate(sql);

            if (codigoRetorno == 1) {
                sucessoUpdate = true;
            }

        } catch (SQLException e) {
            System.out.println("Erro ao atualizar Cliente. Causa: " + e.getMessage());
        } finally {
            Banco.closePreparedStatement(prepStmt);
            Banco.closeConnection(conexao);
        }

        return sucessoUpdate;
    }

    public Cliente encontrarPorId(int id) {
        Cliente cliente = new Cliente();

        Connection conn = Banco.getConnection();
        Statement stmt = Banco.getStatement(conn);
        ResultSet rs = null;

        String query = "SELECT * FROM CLIENTE WHERE IDCLIENTE = " + id;
        try {
            rs = stmt.executeQuery(query);
            if (rs.next()) {
                cliente.setIdCliente(rs.getInt(1));
                cliente.setNome(rs.getString(2));
                cliente.setCpf(rs.getString(3));
                cliente.setEmail(rs.getString(4));
                cliente.setDataDeNascimento(rs.getString(5));
            }
        } catch (SQLException e) {
            System.out.println(
                    "Erro ao executar a Query que verifica existência de Cliente por ID. Erro:" + e.getMessage());
        } finally {
            Banco.closeResultSet(rs);
            Banco.closeStatement(stmt);
            Banco.closeConnection(conn);
        }
        return cliente;
    }

and I’m putting the url http://localhost:8080/api/clients/1 with the json body

  {
    "IDCLIENTE": 1,
    "NOME": "Alice",
    "CPF": "11111111112",
    "EMAIL": "[email protected]",
    "DATADENASCIMENTO": "1998-03-31"
    }

2 answers

1

Try putting this:

Puts @RequestParam("idCliente") int id and @RequestBody Cliente cliente

public Response atualizarCliente(@RequestParam("idCliente") int id, @RequestBody Cliente cliente) throws Exception {
  • Add the Requestbody before the client but error appears when I try to put the Requestparam, is not giving the option to import it

  • @Laissant'ana leaves @PathParam("idCliente") to see if it solves :)

  • Stopped giving null Pointer, but it is not changing the client. Postman is returning 200 but when I get this id nothing has been modified

1


The Nullpointer is probably in the System.out.println(client.toString()) line; that the client object is not arriving in this method.

Checks if the data you are passing in Postman is the same as the method you receive in the case of the Customer.

in the Client parameter puts @Requestbody before, so @Requestbody Client.

  • null Pointer Exception stopped after I took the sysout but even with the request body is not getting the filled object :( In Postman I put the name of the equal variables of java and is returning a 200 ok, but is not updating

  • Now you need to Debug. Before return put a break point at the beginning of the method and see if the Client is filled. If yes the problem is in your methods down, then add more break point until you find the reason. This way you will see how your data sent is behaving.

  • Is the id was not coming filled in the client, tidied up and went!! mt thank you!!!

  • Arrange, a good practice is to always debug to see the behavior of the system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.