gpt4 book ai didi

java - Liferay : Java NullPointer exception

转载 作者:行者123 更新时间:2023-12-01 12:49:15 25 4
gpt4 key购买 nike

我刚刚部署了我的第一个 liferay portlet,但该 portlet 似乎无法工作。这是我在控制台上遇到的错误(我也是 Java 开发和 Liferay 的新手!):

23:22:03,954 ERROR [http-bio-8080-exec-10][render_portlet_jsp:132] null
java.lang.NullPointerException
at me.hicham.portlet_view.Films.doEdit(Films.java:102)

在第 102 行我有:

        if(mode.equalsIgnoreCase("edit")) {...}

这是我的 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> ";



try {
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="";
for (Film film : 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>";

} catch (Exception e) {
// TODO Auto-generated catch 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=" ";
try {

String id_film="0";
id_film = request.getParameter("ID");
Film 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";

} catch (Exception e) {
// TODO Auto-generated catch block
chaineHTML=chaineHTML+e.getMessage();
}
request.setAttribute("att",chaineHTML);

}
else
{
String chaineHTML="";
try {
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";

} catch (Exception e) {
// TODO Auto-generated catch 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 {

try
{
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());
}
}

}

谢谢大家的帮助。

最佳答案

如果您不包含模式参数,这看起来会抛出 NPE。

String mode=request.getParameter("mode"); // This returns null if mode isn't set.
request.setAttribute("att1", mode);

if(mode.equalsIgnoreCase("edit")) // You can't call methods on a null object.

您可以在将模式与“编辑”进行比较之前检查模式是否为空,或者只是交换模式/“编辑”,因为“编辑”永远不会为空

if("edit".equalsIgnoreCase(mode))

关于java - Liferay : Java NullPointer exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24376192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com