2
respostas

NULL POINTER EXCEPTION

Meu servidor está jogando a seguinte exceção:

java.lang.NullPointerException
    at java.base/java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1470)
    at java.base/java.text.DateFormat.parse(DateFormat.java:393)
    at br.com.tuei.acao.CadastroInstituicao.executa(CadastroInstituicao.java:27)
    at br.com.tuei.servlet.UnicaEntradaServlet.service(UnicaEntradaServlet.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:835)
2 respostas

As classes CadastroInstituicao

    package br.com.tuei.acao;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.tuei.modelo.Banco;
import br.com.tuei.modelo.Instituicao;

public class CadastroInstituicao implements Acao {

    public String executa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String novaInstituicao = request.getParameter("nome");
        String paramData = request.getParameter("data");


        Date dataAbertura = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            dataAbertura = sdf.parse(paramData);
        } catch (ParseException e) {
            throw new ServletException(e);
        }

        Instituicao instituicao = new Instituicao();
        instituicao.setNomeInstituicao(novaInstituicao);
        instituicao.setDataAbertura(dataAbertura);


        Banco banco = new Banco();
        banco.adiciona(instituicao);

        request.setAttribute("instituicao", instituicao.getNomeInstituicao());

        return "redirect:entrada?acao=ListaInstituicao";        

    }

}

E a classe

package br.com.tuei.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.tuei.acao.Acao;

@WebServlet("/entrada")
public class UnicaEntradaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String paramAcao = request.getParameter("acao");


        String nome = "br.com.tuei.acao." + paramAcao;


        try {
            Class classe = Class.forName(nome);
            @SuppressWarnings("deprecation")
            Acao acao = (Acao) classe.newInstance();
            nome = acao.executa(request, response);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {

            throw new ServletException(e);
        }

        String[] tipoEndereco = nome.split(":");

        if (tipoEndereco[0].equals("forward")) {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/view/" + tipoEndereco[1]);
            rd.forward(request, response);
        } else {
            response.sendRedirect(tipoEndereco[1]);
        }

    }
}

Não consigo visualizar o motivo do erro.

Desde já agradeço!

Seu código apresenta erro em: at br.com.tuei.acao.CadastroInstituicao.executa(CadastroInstituicao.java:27)

Neste ponto, ele faz o parse de uma data:

dataAbertura = sdf.parse(paramData);

O paramData é recuperado do request.

String paramData = request.getParameter("data");

Assim, verifique se no seu form existe um campo com id="data", ou se está sendo no get ou post um valor de data.