java - How to resolve NullPointerException in JSP -


i developing web application using jsp , servlet , want show records database , number of records in table.

i have created dao in performed queries returned data correctly , put in session in servlet giving name of sessaolistamotoristastodos , totalmotorista.

but when passing values ​​retrieved session , assigning variables, values ​​are not assigned, called variables list listmotoristas , integer totalregistros giving java.lang.nullpointerexception

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", listamotoristas2);    requestdispatcher rd= request.getrequestdispatcher("/listamotoristas2.jsp");    rd.forward(request, response);    } catch (exception e) {      joptionpane.showmessagedialog(null, "erro na servelet"+e);    } } 

jsp:

<% list listamotoristas=(list) request.getattribute("sessaolistamotoristastodos"); integer totalregistros= (integer) request.getattribute("totalmotorista"); int totalpaginas=totalregistros/limite; if(totalregistros%limite!=0){     totalpaginas++; }else{     totalpaginas=0; } %> 

 motoristasdao dao2= new motoristasdao();  list listamotoristas2=dao2.mostrarmotoristas(); 

as per code doing

request.setattribute("sessaolistamotoristastodos", dao2); 

but should

request.setattribute("sessaolistamotoristastodos", listamotoristas2); 

or

request.setattribute("sessaolistamotoristastodos", dao2.mostrarmotoristas()); 

you should set list "listamotoristas2" in request , not "dao2"

and in jsp code follows

list listamotoristas=(list) request.getattribute("sessaolistamotoristastodos"); 

this not return list return dao2


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -