Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How do I resolve this NullPointerException() in updating a photo caption?

    Hi! I recently started learning how to implement MVC. I'm supposed to update a photo caption, but whenever I hit the "Update!" button, an error message appears saying, "The server encountered an internal error () that prevented it from fulfilling this request."

    note: i'm using netbeans 6.5

    This is in the <head> of the my jsp file, editphoto.jsp
    Code:
            <%  if(session.getAttribute("user")!=null)
                {
                    Entities.User user = (Entities.User) session.getAttribute("user");
                    Entities.Album a = (Entities.Album) request.getAttribute("album");
                    Entities.Photo p = (Entities.Photo) request.getAttribute("photo");
                    Entities.User view = user;
            %>
    This is the text field for editing the caption
    Code:
    <input type="text" id="caption" name="caption" value="<%= p.getCaption()%>" onfocus="this.select()">
    This is part of the Controller, "PhotoUpdate.java"
    Code:
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            //PrintWriter out = response.getWriter(); for some reason, the code wouldn't work if I use this.
            try {
             HttpSession s = request.getSession();
    
             Entities.Photo p = (Entities.Photo) s.getAttribute("photos");
             p.setCaption(request.getParameter("caption"));
             Entities.PhotoManager.savePhoto(p);
    
             ServletContext sc = getServletContext();
             RequestDispatcher rd = sc.getRequestDispatcher("/viewphotoprofile.jsp"); //this is supposed to go to the page which displays the photo, with its caption updated
             rd.forward(request, response);
    
            } finally {
                //out.close();
            }
    
        }
    Also, when I retrieved the data from the database into the text field, my other controller (called PhotoUpdatePrepare.java) didn't need PrintWriter. It wouldn't work at all if I used it. It's strange, because when making the code for updating the user profile, PrintWriter is used and it worked. I only copied most of the things needed in updating user profile to the codes in updating a photo caption.

    Again, I'm new to MVC. I'm only a student and this is for our web development class. Any help would be greatly appreciated, and I am still tracing where I could go wrong up until now.

    - Murphy

    error stack from apache tomcat log:

    Mar 31, 2009 8:45:46 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet photoUpdate threw exception
    java.lang.NullPointerException
    at Control.photoUpdate.processRequest(photoUpdate.java:40)
    at Control.photoUpdate.doGet(photoUpdate.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
    Last edited by samespace; 03-31-2009 at 01:35 PM. Reason: adding apache tomcat log

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    First off, the log would be more helpful to us if you pointed out where line 40 is in your file, haha. Second, a NullPointerException occurs when an object is null and then you attempt to access an instance variable or method using this null object. It throws this errors since the object is null and thus cannot access the variable or method that would normally be associated with this. This is important to understand since it will help debug the problem.

    Now, this is my guess as to your line numbering in PhotoUpdate.java:
    Code:
    39: Entities.Photo p = (Entities.Photo) s.getAttribute("photos");
    40: p.setCaption(request.getParameter("caption"));
    If p is null, that next line will throw the NullPointerException. So line 39 is your problem. My guess is that there is no session index called "photos". In your head of the editphoto.jsp page, you do a request.getAttribute for "photo". So is your session index named the same?

    Without seeing the rest of your code, my guess is that either your session index is called "photo" rather than "photos", or you don't even have a session index with either of those names and you should instead be doing a request.getAttribute.

    -Shane


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •