1

Being in a rush for a release, I had to integrate a portion of a JSP made with custom tags with another part, that shows some informations with scriptlest.

To make the two work together, I exposed an object from the tag handler doStartTag() to the request scope, retrieved that object in the scriptlet, and then removed it at the end of the tag handler execution flow, inside doEndTag().

Some code should clarify my situation.

Here's the relevant part of the tag handler:

public int doStartTag() throws JspException {
    // ...

    // obj is a private member
    pageContext.setAttribute("productFromTag", obj, PageContext.REQUEST_SCOPE);

    return EVAL_BODY_INCLUDE;
}

// ...

public int doEndTag() throws JspException {
    pageContext.removeAttribute("productFromTag", PageContext.REQUEST_SCOPE);
    return EVAL_PAGE;
}

And this is the portion of the JSP where the object is retrieved and processed:

<pre:product><!-- this adds the Product instance to the request scope -->
<div class="item">

    <!-- other HTML -->

         <p class="productName"><prod:name></prod:name></p>
         <%
            Product p = (Product) request.getAttribute("productFromTag");
            // Use p

         %>

         <!-- other HTML -->
   </div>
</pre:product>

Everything works fine after the first deploy, but if I replace the JSP (say after an edit), the removeAttribute() method throws a NullPointerException:

Caused by: java.lang.NullPointerException
    at org.apache.jasper.runtime.PageContextImpl.doRemoveAttribute(PageContextImpl.java:398)
    at org.apache.jasper.runtime.PageContextImpl.removeAttribute(PageContextImpl.java:387)
    at xx.xxxxx.xxxx.ProductTag.release(ProductTag.java:89)
    at org.apache.jasper.runtime.TagHandlerPool.release(TagHandlerPool.java:165)
    at org.apache.jsp.modelli.camediResponsive.catalogo.schedaProdotto_jsp._jspDestroy(schedaProdotto_jsp.java:200)
    at org.apache.jasper.runtime.HttpJspBase.destroy(HttpJspBase.java:60)
    at org.apache.jasper.servlet.JspServletWrapper.destroy(JspServletWrapper.java:478)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:166)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    ... 83 more

By reading removeAttribute() javadoc, no exception should be thrown.
Am I misreading the docs? Am I doing something wrong? I usually replace (i.e. redeploy) JSP's, both when editing inside Eclipse or in running production applications, but I've neved faced this situation.

Of course I've already added a catch() around the method invocation, but I'm puzzled about why this is happening.

watery
  • 5,026
  • 9
  • 52
  • 92
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Seth May 04 '16 at 12:57
  • @Seth Did you see the *"Of course I've already added a catch() around the method invocation, but I'm puzzled about why this is happening"* part of my question? – watery May 04 '16 at 13:00
  • Do you understand the purpose of canonical Q&As? – Seth May 04 '16 at 13:06
  • @Seth This is not a question about how to fix an NPE, this is a question about why an NPE is thrown from *that* method, when, to my understanting, it should not be thrown. – watery May 04 '16 at 13:10
  • Okay. Just in case I'm missing out on something, what exactly do you mean with "replacing the JSP"? – Seth May 04 '16 at 13:13
  • @Seth When you edit a JSP inside Eclipse, it automatically *replaces* that resource to the local Tomcat instance; or in a deployed webapp running on a standalone Tomcat instance, if you copy-paste a JSP over another, the JSP deployed within the webapp is *replaced*, effectively showing the new code without the need to stop and start the whole webapp. – watery May 04 '16 at 13:16
  • Okay, that's something that you should add to your question since as of now it's quite unclear (which is why I flagged it initially). Does the NPE occur regardless of what you change? – Seth May 04 '16 at 13:24
  • does `if(pageContext.getRequest() != null)` around the removeAttribute fix it? – tak3shi May 04 '16 at 13:56

0 Answers0