Null Pointer after Try catch

Asked

Viewed 47 times

2

Servlet:

@WebServlet("/AdicionaContato")
public class AdicionaContatoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //busca o writter
        PrintWriter out = response.getWriter();

        //busca parametros
        String nome = request.getParameter("nome");
        String endereco = request.getParameter("endereco");
        String email = request.getParameter("email");
        String dataEmTexto = request.getParameter("DataNascimento");

        //Converter data
        Calendar dataNascimento = null;
        try {
            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
            dataNascimento = Calendar.getInstance();
            dataNascimento.setTime(date);
        } catch (ParseException e){
            out.println("Erro na conversão da data");
            return;
        }
    }
}

After using the SimpleDataFormat and set the date with getInstance / setTime it wasn’t supposed to be populated ?

My HTML page that makes subbmit

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Adicionar contatos</title>
</head>
<body>
      <form action="AdicionaContato">
       Nome : <input type="text" name="nome" /> <br/>
       E-mail: <input type="text" name="email" /> <br/>
       Endereço: <input type="text" name="endereco" /> <br/>
       Data Nascimento: <input type="text" name="dataNascimento" /><br/>
       <input type="submit" value="Gravar">
</form>
</body>
</html>

1 answer

6


The name of the birth date input is different from the name used in the getParameter(String).

One is like this: dataNascimento, and the other one is like this: DataNascimento the getParameter is Case Sensitive.

Check that this is not the cause of the problem.

  • that’s right, thank you

Browser other questions tagged

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