java.lang.NullPointerException JSP Servlet

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 where I performed queries where they returned the data correctly and put a session in a servlet giving the name of sessionListMotoristsAll and totalMotorist but when passing the values retrieved from the session and assign the variables, the values are not assigned, the called variables are List listaMotoristas and Integer totalRegistros giving an error of java.lang.NullPointerException / p>

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;
}
     %>
    
asked by anonymous 14.09.2017 / 06:31

2 answers

1

Some comments:

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

        JOptionPane.showConfirmDialog(null,e);
    
  • You should not use two try..catch blocks in the same method as you are doing, because in the first case, if a connection error occurs with the database, it will "log" the error and continue executing, that is, it will give error in the subsequent lines. Put everything in a single try..catch block please.

14.09.2017 / 11:40
0

You're looking for the wrong name. This set up "totalMotoristas" plural.

request.setAttribute("totalMotoristas", totalMotorista);

And trying to recover as singular "totalMotorist."

Integer totalRegistros= (Integer) request.getAttribute("totalMotorista");
    
15.09.2017 / 17:50