3
respostas

Não consigo atualizar meu Objeto usando Spring MVC

Já consigo recuperar meu objeto da minha tabela para meu formulário, hora que vou persistir estou com erro.

Segue meu codigo.

Controller

/**
     * Método que recuprra a Tarefa Selecionada
     * @param tarefa
     * @param model
     * @return
     */
    @RequestMapping("mostraTarefaSelecionada")
    public String mostraFormularioAtualizacao(Tarefa tarefa,Model model){
        try {
            Connection connection = new ConnectionFactory().getConnection();
            TarefaDAO dao = new TarefaDAO(connection);
            model.addAttribute("tarefa", dao.buscaPorId(tarefa.getId()));
            return "tarefa/mostra";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Após recuperar, atualiza o Objeto
     * @param id
     * @param model
     */
    @RequestMapping("atualizarTarefaSelecionada")
    public String buscaTarefa(Tarefa tarefa){
        try {
            Connection connection = new ConnectionFactory().getConnection();
            TarefaDAO dao = new TarefaDAO(connection);
            dao.atualizarTarefa(tarefa);
            return "redirect:listarTarefas";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

DAO

public void atualizarTarefa(Tarefa tarefa) {
        String sql = "UPDATE tarefas SET descricao = ?, finalizado = ?, data_finalizacao = ? WHERE id = ?";

        try {
            PreparedStatement prepareStatement = conn.prepareStatement(sql);
            prepareStatement.setString(1, tarefa.getDescricao());
            prepareStatement.setBoolean(2,tarefa.isFinalizado());
            prepareStatement.setDate(3, new Date(tarefa.getDataFinalizacao().getTimeInMillis()));
            prepareStatement.setLong(4, tarefa.getId());

            prepareStatement.execute();
            prepareStatement.close();
            conn.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

meu Fomulário

<h3>Alterar tarefa - ${tarefa.id}</h3>

    <form action="atualizarTarefaSelecionada" method="post"> 
        <input type="hidden" name="id" value="${tarefa.id}">

        Descrição:<br />
        <textarea rows="6" cols="18" name="descricao" >    ${tarefa.descricao} </textarea><br /> 

        Finalizado? <br/>
        <input type="checkbox" name="finalizado" value="true" ${tarefa.finalizado ? 'checked' : '' } /> <br />
        Data Finalizacao:
        <input type="text" name="dataFinalizacao" value="<fmt:formatDate value="${tarefa.dataFinalizacao.time}" pattern="dd/MM/yyyy" />" /> <br /> 
        <input type="submit" value="Enviar">
    </form>

Meu Erro

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.RuntimeException: java.lang.NullPointerException
    br.com.caelum.tarefas.controller.TarefasController.buscaTarefa(TarefasController.java:124)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException
    br.com.caelum.tarefas.model.TarefaDAO.atualizarTarefa(TarefaDAO.java:114)
    br.com.caelum.tarefas.controller.TarefasController.buscaTarefa(TarefasController.java:121)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
3 respostas

E aí, Dayson! Tudo certo, cara? =)

Você pode colocar o código do seu Controller aqui, por favor? Você está tomando uma NullPointerException que envolve tanto essa classe, quanto seu DAO... =|

Fábio

Pronto, acabei de incluir o Controller.

alguém???