nullpointerexception - Liferay : Java NullPointer exception -
i deployed first liferay portlet, portlet not seem work. error on consol (i'm new java developement , liferay !) :
23:22:03,954 error [http-bio-8080-exec-10][render_portlet_jsp:132] null java.lang.nullpointerexception @ me.hicham.portlet_view.films.doedit(films.java:102)
in line 102 have :
if(mode.equalsignorecase("edit")) {...}
here films.java :
package me.hicham.portlet_view; import me.hicham.portlet_controller.film; import me.hicham.portlet_model.*; import javax.portlet.actionrequest; import javax.portlet.portletmode; import javax.portlet.portleturl; import javax.portlet.renderrequest; import javax.portlet.actionresponse; import javax.portlet.renderresponse; import javax.portlet.portletexception; import javax.portlet.windowstate; import com.liferay.util.bridges.mvc.mvcportlet; import java.io.ioexception; import javax.portlet.portletrequestdispatcher; public class films extends mvcportlet { public void doview(renderrequest request, renderresponse response) throws portletexception, ioexception { string nomfilm = request.getparameter("nom"); if (nomfilm == null ) { nomfilm=""; } string listefilm=""; portleturl renderurl = response.createrenderurl(); renderurl.setportletmode(portletmode.view); renderurl.setwindowstate(windowstate.maximized); renderurl.setwindowstate(windowstate.normal); listefilm=listefilm+"<div id=\"container\">"; listefilm=listefilm+"<form name=\"input\" action=\""+renderurl+"\" method=\"post\" class=\"zebra\">"; listefilm=listefilm+"<table>"; listefilm=listefilm+"<tr><td>nom:</td><td><input type=\"text\" name=\"nom\" value=\""+nomfilm+"\"></td></tr>"; listefilm=listefilm+"<tr><td></td><td><input type=\"submit\" value=\"rechercher\"></td></tr>"; listefilm=listefilm+"<table>"; listefilm=listefilm+"</form> "; seek { listefilm=listefilm+"\n<table class=\"zebra\">"; listefilm=listefilm+"\n<tr>"; listefilm=listefilm+"\n<td></td><td> nom </td><td> genre </td><td> synopsis </td><td> date sortie </td>"; listefilm=listefilm+"\n</tr>"; string id=""; (film cinema : mediadao.listertouslesfilms(nomfilm)) { id=string.valueof(film.getid_film()); portleturl renderurl2 = response.createrenderurl(); renderurl2.setportletmode(portletmode.edit); renderurl2.setwindowstate(windowstate.maximized); renderurl2.setwindowstate(windowstate.normal); renderurl2.setparameter("mode","edit"); renderurl2.setparameter("id",id); listefilm=listefilm+"\n<tr>"; listefilm=listefilm+"\n<td><a href=\""+renderurl2+"\"><img src=\"http://localhost:8080/html/themes/classic/images/common/edit.jpg\" alt=\"modifier\"/></a></td><td>"+film.getnom()+"</td><td>"+ mediadao.getgenre(film.getid_genre()).getlibelle()+"</td><td>"+film.getsynopsis()+"</td><td>"+film.getdatesortie()+"</td>"; listefilm=listefilm+"\n</tr>"; } listefilm=listefilm+"\n</table>"; portleturl renderurl2 = response.createrenderurl(); renderurl2.setportletmode(portletmode.edit); renderurl2.setwindowstate(windowstate.maximized); renderurl2.setwindowstate(windowstate.normal); renderurl2.setparameter("mode","add"); listefilm=listefilm+"\n<a href=\""+renderurl2+"\"><img src=\"http://localhost:8080/html/themes/classic/images/common/ajout.jpg\" alt=\"ajouter\"/></a>"; listefilm=listefilm+"</div>"; } grab (exception e) { // todo auto-generated grab block listefilm=listefilm+e.getmessage(); } request.setattribute("att",listefilm); response.setcontenttype("text/html"); portletrequestdispatcher dispatcher = getportletcontext().getrequestdispatcher("/web-inf/jsp/films_view.jsp"); dispatcher.include(request, response); } public void doedit(renderrequest request, renderresponse response) throws portletexception, ioexception { response.setcontenttype("text/html"); string mode=request.getparameter("mode"); request.setattribute("att1", mode); if(mode.equalsignorecase("edit")) { string chainehtml=" "; seek { string id_film="0"; id_film = request.getparameter("id"); cinema film = mediadao.getfilm(id_film); chainehtml=chainehtml+"\n<table class=\"zebra\">"; chainehtml=chainehtml+"\n<tr>"; chainehtml=chainehtml+"\n<td colspan=\"2\">modification</td>"; chainehtml=chainehtml+"\n</tr>"; chainehtml=chainehtml+"\n<tr><td><input type=\"hidden\" name=\"mode\" value=\"edit\"><input type=\"hidden\" name=\"id\" value=\""+film.getid_film()+"\">nom :</td><td><input type=\"text\" name=\"nom\" value=\""+film.getnom()+"\"></td></tr>"; chainehtml=chainehtml+"\n<tr><td>genre :</td><td><input type=\"text\" name=\"genre\" value=\""+ mediadao.getgenre(film.getid_genre()).getlibelle()+"\"></td></tr>"; chainehtml=chainehtml+"\n<tr><td>synopsis :</td><td><input type=\"text\" name=\"synopsis\" value=\""+film.getsynopsis()+"\"></td></tr>"; //chainehtml=chainehtml+"\n<tr><td>image :</td><td><input type=\"text\" name=\"image\" value=\""+film.getimage()+"\"></td></tr>"; chainehtml=chainehtml+"\n<tr><td>date sortie :</td><td><input type=\"text\" name=\"datesortie\" value=\""+film.getdatesortie()+"\"></td></tr>"; chainehtml=chainehtml+"\n<tr><td></td><td><input type=\"submit\" value=\"valider\"></td></tr>"; chainehtml=chainehtml+"\n</table>"; chainehtml=chainehtml+"\n"; } grab (exception e) { // todo auto-generated grab block chainehtml=chainehtml+e.getmessage(); } request.setattribute("att",chainehtml); } else { string chainehtml=""; seek { chainehtml=chainehtml+"\n<table class=\"zebra\">"; chainehtml=chainehtml+"\n<tr>"; chainehtml=chainehtml+"\n<td colspan=\"2\">insertion</td>"; chainehtml=chainehtml+"\n</tr>"; chainehtml=chainehtml+"\n<tr><td><input type=\"hidden\" name=\"mode\" value=\"add\"><input type=\"hidden\" name=\"id\" >nom :</td><td><input type=\"text\" name=\"nom\" ></td></tr>"; chainehtml=chainehtml+"\n<tr><td>genre :</td><td><input type=\"text\" name=\"genre\" ></td></tr>"; chainehtml=chainehtml+"\n<tr><td>synopsis :</td><td><input type=\"text\" name=\"synopsis\" ></td></tr>"; //chainehtml=chainehtml+"\n<tr><td>image :</td><td><input type=\"text\" name=\"image\" ></td></tr>"; chainehtml=chainehtml+"\n<tr><td>date sortie :</td><td><input type=\"text\" name=\"datesortie\" ></td></tr>"; chainehtml=chainehtml+"\n<tr><td></td><td><input type=\"submit\" value=\"valider\"></td></tr>"; chainehtml=chainehtml+"\n</table>"; chainehtml=chainehtml+"\n"; } grab (exception e) { // todo auto-generated grab block chainehtml=chainehtml+e.getmessage(); } request.setattribute("att",chainehtml); } portletrequestdispatcher dispatcher = getportletcontext().getrequestdispatcher("/web-inf/jsp/films_edit.jsp"); dispatcher.include(request, response); } public void dohelp(renderrequest request, renderresponse response) throws portletexception, ioexception { response.setcontenttype("text/html"); portletrequestdispatcher dispatcher = getportletcontext().getrequestdispatcher("/web-inf/jsp/films_help.jsp"); dispatcher.include(request, response); } public void processaction(actionrequest request, actionresponse response) throws portletexception, ioexception { seek { system.out.println("processaction"); string mode = request.getparameter("mode"); if(mode.equalsignorecase("edit")) { string id_film = request.getparameter("id"); string nom = request.getparameter("nom"); string genre = request.getparameter("genre"); string synopsis = request.getparameter("synopsis"); //string image = request.getparameter("image"); string datesortie = request.getparameter("datesortie"); system.out.println("begin...................."); mediadao._edit(id_film, nom, genre, synopsis, datesortie); system.out.println("end.................."); } else { string nom = request.getparameter("nom"); string genre = request.getparameter("genre"); string synopsis = request.getparameter("synopsis"); //string image = request.getparameter("image"); string datesortie = request.getparameter("datesortie"); system.out.println("begin...................."); mediadao._add( nom, genre, synopsis, datesortie); system.out.println("end.................."); } response.setportletmode(portletmode.edit); }catch (exception e) { e.printstacktrace(); system.err.println("process action err "+e.getmessage()); } } }
thank guys help.
this looks throw npe if don't include mode parameter.
string mode=request.getparameter("mode"); // returns null if mode isn't set. request.setattribute("att1", mode); if(mode.equalsignorecase("edit")) // can't phone call methods on null object.
you check if mode null before comparing "edit" or swap mode/"edit" since "edit" never null
if("edit".equalsignorecase(mode))
java nullpointerexception liferay
No comments:
Post a Comment