Issue
In my ImageUtility.jsp where I display images in a table form after getting details from databse, I have an edit form(within ImageUtility.jsp) which will pass the id of Image to doGet() in ImageEdit.java, which after getting that attribute send it to ImageEdit.jsp and where I am trying to access that id attribute along with new name entered by user.
ImageUtility.jsp
for (Images i : li) {
id = i.getId() + "";
name = i.getName();
size = i.getSize() + " kb";
preview = i.getImagePath();
System.out.println(id+" "+name+" "+size);
pageContext.setAttribute("id", id);
pageContext.setAttribute("name", name);
pageContext.setAttribute("size", size);
%>
<tr>
<td>${id}</td>
<td class='ImgName'>${name}</td>
<td>${size}</td>
<td><img width='150px' height='150px' src='" + preview + "'></td>
<td><form action="ImageEdit" method="get"><input type="hidden" name="id" value=${id} /><input type="submit" value="Edit"/></form></td>
<td><form action="ImageDelete"><input type="hidden" value=${id} /><input type="submit" value="Delete" /></form></td>
</tr>
ImageEdit.java
@WebServlet("/ImageEdit")
public class ImageEdit extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=(int) request.getAttribute("id");
System.out.println(id);
//request.setAttribute(name, o);
request.setAttribute("imageId", id);
request.getRequestDispatcher("ImageEdit.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int imageId = (int) request.getAttribute("imageId");
// int imageId = Integer.parseInt(str) ;
String str = request.getParameter("name");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Images img = (Images) session.get(Images.class, imageId);
img.setName(str);
try {
session.update(img);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
session.close();
PrintWriter out = response.getWriter();
out.print("error");
return ;
}
session.close();
response.sendRedirect("./ImageUtility.jsp");
}
}
Imageedit.jsp
<html>
<head>
</head>
<body>
<div id ="overlay"></div>
<form method="post" action="ImageEdit" >
<fieldset>
<legend >Change Image Name</legend>
<label>Enter Name : <Input name="name" required></label><br><br>
<input type="submit">
</fieldset>
</form>
</body>
</html>
When I click on Edit button on here
<td><form action="ImageEdit" method="get"><input type="hidden" name="id" value=${id} /><input type="submit" value="Edit"/></form></td>
in ImageUtility.jsp I am passing in the id of Image that I want to delete to doGet() method but I a null pointer exception in doGet() method. I can't figure out what is causing that
java.lang.NullPointerException
com.package.ImageUtilityApp.controllers.ImageEdit.doGet(ImageEdit.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Any suggestions would be helpful
Solution
I'm fairly confident that form values for a 'GET' form get turned into query parameters instead of attributes.
Try changing your doGet()
method from:
int id=(int) request.getAttribute("id");
to:
int id=(int) request.getParameter("id");
Side Note
I'm fairly confident that you cannot cast a String
to and int
like that.
You'll most like need to do the following:
int id = Integer.parseInt(request.getParameter("id"));
But, if that id
value isn't passed in, you'll get a null pointer exception again, so you might need to do a null check first... And then maybe some validation that the input value is a number... etc...
Answered By - hooknc
Answer Checked By - Mary Flores (JavaFixing Volunteer)