java.lang.Nullpointerexception JSP Servlet

Asked

Viewed 677 times

1

I’m developing a web application using jsp and Servlet and I want to show all my records from my database and the amount of records in a table, I created a DAO in which I performed the queries in which return the data correctly and put in a session in a Servlet giving the name of sessionListaMotoristasAll and totalMotorist but when passing the recovered values of the session and assigning the variables, the values are not assigned, the various calls are List listaMotoristas and Integer totalRegistros making a mistake of java.lang.NullPointerException

UPDATE

   org.apache.jasper.JasperException: An exception occurred processing JSP page /listaMotoristas2.jsp at line 49

   46: 
   47: List  listaMotoristas=(List)  
   request.getSession().getAttribute("sessaoListaMotoristasTodos");
   48: Integer totalRegistros= (Integer)  
   request.getSession().getAttribute("totalMotorista");
   49: int totalPaginas=totalRegistros/limite;
   50: if(totalRegistros%limite!=0){
   51: totalPaginas++;
    52: }
 Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrappe r.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 root cause

java.lang.NullPointerException
org.apache.jsp.listaMotoristas2_jsp._jspService(listaMotoristas2_jsp.java:111)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

 public Integer totalRegistros(String pesquisa){

            try {
        con = Conecta.conexao();
       String sql="Select count(*) as contaRegistros from tb_motorista where mo_nome like '%"+pesquisa+"%'";
        Statement statement = con.createStatement();     

       ResultSet rs = statement.executeQuery(sql); 

        rs.next();
        JOptionPane.showMessageDialog(null,rs.getString("contaRegistros"));
        System.out.println(rs.getString("contaRegistros"));
        Integer 
      totalRegistros=Integer.parseInt(rs.getString("contaRegistros"));
        return totalRegistros;

    } catch (Exception e) {
        JOptionPane.showConfirmDialog(null,e);

}
            return 0;
}



public List<Motoristas> mostrarMotoristas(){

    try { 
    con = Conecta.conexao();

       String sql="Select * from tb_motorista ";
        Statement statement = con.createStatement();     

    ResultSet rs = statement.executeQuery(sql); 
      List lista = new ArrayList(); 


        while(rs.next()){
             Motoristas mo= new Motoristas();
            mo.setMo_nome((rs.getString("mo_nome")));
            mo.setMo_data(rs.getString("mo_data"));
            mo.setMo_cpf(rs.getString("mo_cpf"));
            mo.setMo_modelo(rs.getString("mo_modelo"));
            mo.setMo_status(rs.getString("mo_status"));
            mo.setMo_sexo(rs.getString("mo_sexo"));


           lista.add(mo);

        }

        return lista;
    } catch (Exception e) {
        JOptionPane.showConfirmDialog(null,e);
        return null;
    }

}


       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     MotoristasDAO dao= new MotoristasDAO();
         MotoristasDAO dao2= new MotoristasDAO();

   String pesquisa=request.getParameter("pesquisa");
     try {
     if(pesquisa==null){
    pesquisa="";
    } 


       Integer totalMotorista=dao.totalRegistros(pesquisa);
       request.setAttribute("totalMotoristas", totalMotorista);


           List listaMotoristas2=dao2.mostrarMotoristas();


          request.setAttribute("sessaoListaMotoristasTodos", dao2); 


     RequestDispatcher rd= 
    request.getRequestDispatcher("/listaMotoristas2.jsp");
    rd.forward(request, response);

    } catch (Exception e) {
         JOptionPane.showMessageDialog(null, "Erro na servelet"+e);
    }

     }

     <%
  List  listaMotoristas=(List) request.getAttribute("sessaoListaMotoristasTodos");
  Integer totalRegistros= (Integer) request.getAttribute("totalMotorista");
 int totalPaginas=totalRegistros/limite;
 if(totalRegistros%limite!=0){
 totalPaginas++;
 }
  else{
totalPaginas=0;
}
     %>
  • Adds the stack of errors.

  • Two things: 1. Add here the error stack trace to identify the specific location where it occurs 2. You should not use swing library in Rvlets because it will not work, like here: } catch (Exception e) { Joptionpane.showConfirmDialog(null,e); }

  • The line con = Conecta.conexao(); works? I mean, the variable con is no longer null after the execution of that line?

  • Igor Venturelli works perfectly, debugged the code and the methods that search the database return the data correctly

  • I gave an UPDATE on the question and put the full stack trace of the error

2 answers

1

Some observations:

  • Add the error stack trace to identify the specific location where it occurs, as it is very difficult to infer;
  • You should not use swing library in Rvlets because it will not work, as here:

        JOptionPane.showConfirmDialog(null,e);
    
  • You should not use two Ry blocks.. catch in the same method as it is doing, because in the first one, if there is an error connected to the bank it will "log in" the error and continue running, that is, it will give error in the subsequent lines. Put everything in single block Try.. catch please.

  • put the full trace stack of the bug, and left with only a Try catch

  • even letting only a Try catch error persisted the same

  • Yes, Try.. catch unico would not solve your problem as the error is in your JSP file, line 49. Possibly the total variableRegisters is null. See if Voce is returning it correctly in Servlet’s Sponse

  • this I am asking you, man, when picking up the Answer of Servlet it does not return the total of records, and I debugged the code of the DAO queries and return the values correctly, and I am taking the name of the Answer of Servlet correctly, I don’t know why it didn’t work

0

You are looking for the wrong name. This set "totalMotoristas" plural.

request.setAttribute("totalMotoristas", totalMotorista);

And trying to recover as "totalMotorista" singular.

Integer totalRegistros= (Integer) request.getAttribute("totalMotorista");
  • changed and plural but continued the same error

  • already debugged the code several times it returns the database result, but when I assign the request.getattribute("") to Interger totalRegistros it is null

  • Something is wrong with the stack trace code, because it is different from the code that Voce posted here. No of the trace stack Voce is trying to get the value of the session (request.getSession().getattribute(...)). It’s confusing.

Browser other questions tagged

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