I am trying to access attribute which I passed but it's giving me a null pointer exception

Akhil Garg

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

hooknc

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am struggling finding out why am I getting a null pointer exception. I know it's because of the null but I can't seem find which one

I am getting null pointer exception when I an trying to putExtra from intent

Why am I getting a null pointer access?

I am trying to make a pointcut. but its giving an Exception

I am trying to run a task in my async method but it's giving me a ui thread error

I am trying to call a function from useEffect hook and it's giving me undefined

I am trying to get the top 5 products using a Mysql Query but it's giving me an error

I am trying to build my game in unity but it is giving me an error

I am trying to get a reference to the ResourceResolver from the ResourceResolverFactory but got Null pointer Exception

why am i getting null pointer exception when trying to set a value to "rating" in my rating bar?

why am I getting Null Pointer Exception while trying to check if the username is already present in the database?

I am trying to renew my facebook access token for posting on my wall automatically but getting null exception why?

I am trying to use a bottom sheet modal in flutter conditionally only when users are not subscribed but it's giving me errors

Why am I getting a null pointer exception in my iterator?

Servet - Why am i getting null pointer exception?

I am getting Null Pointer Exception in GridView Android

Is there a way to get rid of this null pointer exception I am geting?

I have an 'if' statement for checking null pointer exception but that line itself is giving a null pointer excpetion

Null pointer when trying to access POJO attribute

I am trying to access the camera

I am trying to use amazon api code in php but it is giving me error of not supported version following is my code

I am trying to publish the App into playstores but its giving me for violating our dangerous products policy

i am trying to print words from a sentence in c++ but its giving me Segmentation Fault(core dump)

I am trying to use sharedPreferences from my Register Page and retrieve it on another class, but it is giving me error in the logcat

I am trying to update a value in an observable every second in setInterval but mobx is giving me error

I am trying to create an embed but it keeps giving me an invalid syntax with the variable name

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

I am trying to alter a type in object relational model to add a member function to it, but it keeps giving me errors

I am trying to loop inserting elements into a set if they intersect, but it isn't giving me what im looking for