java.lang.Nullpointerexception when passing to Servlet

Asked

Viewed 127 times

0

Good morning I’m having a hard time passing the data of a jsp to Servlet that will insert the data in the database, I made three forms for scheduling and in the third it will send to Servlet but it appears the error java.lang.Nullpointerexception

Jsp-----------------------



 <!DOCTYPE html>
        <%
        String cpf = request.getParameter("cpf");
        Cliente c = new Cl

iente();
    try{
       c.setCpf(cpf);
       c.carregar();
    }catch(Exception e){
        out.print("Erro:"+e);
    }

    %>
    <%
    String idservico = request.getParameter("idservico");
    Servico s = new Servico();
    try{
       s.setIdservico(Integer.parseInt(idservico));
       s.carregar();
    }catch(Exception e){
        out.print("Erro:"+e);
    }

    %>
    <%
        String funcionario_cpf = request.getParameter("funcionario_cpf");
    Funcionario f = new Funcionario();
    try{
       f.setCpf(funcionario_cpf);
       f.carregar();
    }catch(Exception e){
        out.print("Erro:"+e);
    }

    %>
    <%
    String data = request.getParameter("data");
    Agendamento a = new Agendamento();
    ArrayList<Agendamento> agendados=new ArrayList<Agendamento>();
    DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
    try{
      agendados =  a.listarPorDataCPF( df.parse(data), funcionario_cpf);
    }catch(Exception e){
        out.print("Erro:"+e);
    }

    String[] horarios = {"09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30"};

    %>

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>

    <script>
    function goBack() {
        window.history.back();
    }
    </script> 
        </head>
        <body>
             <%@include file="menu.jsp" %>
            <center>
            <h1>Registrar Agendamento do Cliente</h1>
            <form action="gerenciar_agendamento.do" method="post" id="form_agenda">
                <input type="hidden" name="op" value="inserir"/>
                <b>Cpf :</b><%=c.getCpf() %> /
                <b>Nome do Cliente:</b><%=c.getNome() %><br/>
                <b>Servico:</b> <%=s.getNome() %> (R$<%=s.getPreco() %>)<br/>
                <b>Funcionário</b> <%=f.getNome()%><br/>
                <b>Data:</b> <%=df.parse(data).toString() %><br/>
                <%
                for(int i=0;i<horarios.length;i++){
                    boolean existe=false;
                    for(Agendamento ag:agendados){
                        if(ag.getHora().toString().equals(horarios[i]+":00")){
                            existe=true;
                        }
                    }
                    if(existe){
                        out.print(horarios[i]+" (indisponível)<br/>");
                    }else{
                        out.print(horarios[i]+"<input type='radio' name='horario'/><br/>");
                    }

                }
                %>
                <input type='button' value='Voltar' onclick="javascript: history.go(-1)" />
                </select><input type="submit" value="próximo" /><br/>
                <br><br><br><br>
                </center>
        </body>
    </html>

Servlet-----------------

String op = request.getParameter("op");
            String idagendamento = request.getParameter("idagendamento");
            String data = request.getParameter("data");
            String hora = request.getParameter("hora");
            String valor = request.getParameter("valor");
            String situacao_pagamento = request.getParameter("situacao_pagamento");
            String situacao_servico = request.getParameter("situacao_servico");
            String cliente_cpf = request.getParameter("cliente_cpf");
            String servico_idservico = request.getParameter("servico_idservico");
            String funcionario_cpf = request.getParameter("funcionario_cpf");            
            try {
                Agendamento a = new Agendamento();
                switch (op) {
                    case "inserir":{
                        if(data.equals("") || data.isEmpty()){
                            out.print("O campo DATA deve ser preenchido!");
                        }else if(hora.equals("") || hora.isEmpty()){
                            out.print("O campo HORA deve ser preenchido!");
                        }else if(valor.equals("") || valor.isEmpty()){
                            out.print("O campo VALOR deve ser preenchido!");                            
                        }else if(situacao_pagamento.equals("") || situacao_pagamento.isEmpty()){
                            out.print("O campo SITUAÇÃO PAGAMENTO deve ser preenchido!");
                        }else if(situacao_servico.equals("") || situacao_servico.isEmpty()){
                            out.print("O campo SITUAÇÃO SERVIÇO deve ser informada!");
                        }else if(cliente_cpf.equals("") || cliente_cpf.isEmpty()){
                            out.print("O campo CLIENTE deve ser selecionado!");
                        }else if(servico_idservico.equals("") || servico_idservico.isEmpty()){
                            out.print("O campo SERVIÇO deve ser selecionado!");
                        }else if(funcionario_cpf.equals("") || funcionario_cpf.isEmpty()){
                            out.print("O campo FUNCIONÁRIO deve ser selecionado!");
                        }else{

                            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");                
                            format.format( new Date()   );                    
                            a.setData(format.parse(data));


                            SimpleDateFormat formatador = new SimpleDateFormat("HH:mm");
                            Date horaa = formatador.parse(hora);
                            Time time = new Time(horaa.getTime());
                            a.setHora(time);  


                            a.setValor(Double.parseDouble(valor));
                            a.setSituacao_pagamento(Boolean.parseBoolean(situacao_pagamento));
                            a.setSituacao_servico(Boolean.parseBoolean(situacao_servico));
                            Cliente c = new Cliente();
                            c.setCpf(cliente_cpf);
                            Servico s = new Servico();
                            s.setIdservico(Integer.parseInt(servico_idservico));
                            Funcionario f = new Funcionario();
                            f.setCpf(funcionario_cpf);
                            a.registrarAgendamento();
                            response.sendRedirect("listar_agendamento.jsp");
                        }
                    }

Please whoever can help me, I’m having this difficulty, and this is the mistake I mentioned earlier

Glassfish Server Output--------,

Information: Created HTTP Listener http-Listener-1 on host/port 0.0.0.0:8080 Information: Grizzly Framework 2.3.23 Started in: 9ms - bound to [/0.0.0.0:8080] Information: Jmxstartupservice has Started Jmxconnector on Jmxservice URL service:jmx:rmi://DESKTOP-0K4LIKA:8686/jndi/rmi://DESKTOP-0K4LIKA:8686/jmxrmi Information: Dt:Thu Nov 23 00:00:00 BRST 2017

I put the end, because the only line that is added when the error occurs is just the last one, the error of java.lang.Nullpointerexception appears in the browser when the data is redirected to Servlet(manage scheduling.do), test for an out.print after the case "insert" and the message appeared in the browser, the data that are not being entered I think

The only message that appears, error that is shown in the browser is this: Error: java.lang.Nullpointerexception, nothing else

  • Has several lines liable to NullPointerException. Edit the question and add the console output, please

  • Friend, forgive the ignorance, but the exit from the console what would it be? Because I use Netbeans and output if that’s what I understand, it would be the Glassfish Server field?

  • @Matheusoliveira, precisely. Show the output of the web server in which the error is being displayed.

  • @Andrewribeiro Error occurs when passing to Servlet, and it is shown in the browser, went to the output in Glassfish and shows no information regarding the error, I think the error is when passing the information of the forms to Servlet, are 3 jsp in all, and in each one I have been saving the scheduling choices to then be passed on to manage scheduling.

  • @Matheusoliveira, every error is saved in the log. Even the error that appears in the browser is also saved in the log. But ok, if you are having trouble finding the correct log, copy all the error in the browser, edit the question and paste the error.

  • @Andrewribeiro I did this, I copied all the error shown in the browser which is only the one that is shown

  • I saw that you edited the question. In this case, there is not much to escape: you have to go debugging and see if some variable contains the value null.

  • I did it, and they’re all being passed over, I don’t know what can be, but thank you

Show 3 more comments
No answers

Browser other questions tagged

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