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.
– dhb
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); }
– Gyowanny Pessatto Queiroz
The line
con = Conecta.conexao();
works? I mean, the variablecon
is no longer null after the execution of that line?– igventurelli
Igor Venturelli works perfectly, debugged the code and the methods that search the database return the data correctly
– User1999
I gave an UPDATE on the question and put the full stack trace of the error
– User1999