Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Help! pageContext is NULL in custom tag constructor!

843833Oct 11 2001 — edited Oct 12 2001
I'm having a problem with a custom tag class, and I'm not sure whether it's because I'm doing something wrong, or because I'm running into a bug with Tomcat, JDK1.4b2, or something else. Basically, I'm finding that the pageContext object is null in the constructor of a custom tag class... something that I'm pretty sure is NOT supposed to happen.

I'm running JDK1.4b2 with Forte CE 3.1 (the bug predates the jumbo fix that brought it up to 3.0) and using Forte's embedded Tomcat.

I'm not sure whether it matters, but here's the sequence of events:

A servlet gets launched,
instantiates an object of type Error_list,
binds Error_list to the request object, and
forwards to a JSP that uses the custom tag defined by ListErrorsTag.
Unfortunately, pageContext is null in ErrorListTag's constructor, so the attempt to get the request object from the pageContext object generates a NullPointerException.

The SERVLET: *****************************************
// relevant lines from the servlet instantiating the object, binding it, and forwarding...

     Error_list errors = new Error_list("BAD_THINGS", "crash and burn");
     request.setAttribute("errors",errors);

     ServletContext sc = this.getServletContext();
     RequestDispatcher rd = sc.getRequestDispatcher("admin_category_create.jsp");
     rd.forward(request,response);
The JSP: ************************************************
In the JSP "admin_category_create.jsp" itself, I specify the taglib:
     
     <%@ taglib uri='/WEB-INF/AdminTags.tld' prefix = 'admin' %>
and reference it:
     <admin:listErrors>The errors will be listed here</admin:listErrors>
The Explosion of the Taglib: ************************
// beginning of Taglib class:

public class ListErrorsTag extends BodyTagSupport {
    
    private Error_list errors;
    
    public ListErrorsTag() {
    super()
    
    try {
    
         if (pageContext == null)
             System.out.println("uh oh! pageContext is null");
         
         ServletRequest request = pageContext.getRequest();
         
         errors = (Error_list)request.getAttribute("errors");
    
    }
    catch (NullPointerException e) {
         System.out.println("boom!");
    }
}
The Result: **************************************

Tomcat's error log:


uh oh! pageContext is null
boom!

*************************************
if I eliminate the try/catch and allow the NullPointerException to take place, I get:


Error: 500
Location: /admin_category_create.jsp
Internal Servlet Error:

javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:459)
at _0002fadmin_0005fcategory_0005fcreate_0002ejspadmin_0005fcategory_0005fcreate_jsp_0._jspService(_0002fadmin_0005fcategory_0005fcreate_0002ejspadmin_0005fcategory_0005fcreate_jsp_0.java:338)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at org.netbeans.modules.web.tomcat.JspServlet.service(JspServlet.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.facade.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:194)
at admin.processRequest(admin.java:126)
at admin.doPost(admin.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:539)

Root cause:
java.lang.NullPointerException
at AdminTags.ListErrorsTag.(ListErrorsTag.java:32)
at _0002fadmin_0005fcategory_0005fcreate_0002ejspadmin_0005fcategory_0005fcreate_jsp_0._jspService(_0002fadmin_0005fcategory_0005fcreate_0002ejspadmin_0005fcategory_0005fcreate_jsp_0.java:272)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at org.netbeans.modules.web.tomcat.JspServlet.service(JspServlet.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.facade.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:194)
at admin.processRequest(admin.java:126)
at admin.doPost(admin.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:539)

Comments

843833
Doing a little more experimentation, I discovered that pageContext is null in the constructor, but not null in the otherDoStartTagOperations() method (called by the doStartTag() method of the tag's class... it's something Forte forces you to do).

Is this normal? I was under the impression that pageContext is supposed to be defined EVERYWHERE in any class that extends BodyTagSupport... including the class' own constructor.

On a related topic, is there documentation somewhere as to what, exactly, Forte is doing behind the scenes when it's managing a taglib (how it keeps track of them, where it puts config files, how the entries it makes in them are different or extended from the normal layout, etc.)? At the moment, I suspect that half of the grief I'm having with writing taglibs is caused by Forte itself forcing me to do things in a roundabout way that bears little resemblance to the ways shown in different books on the topic (and in fact all but ensures that nearly every published example will fail and require major rewriting because of the way it forces tag classes to be structured), but I don't see any easy way to let Forte handle compiling the classes and testing them with its embedded Tomcat, but do the config file housekeeping myself I can be in control of it.
843833
The pageContext instance variable in your tag handler is not set until after the constructor is called. The sequence of events that occurs in the jsp service method is:

Tag t = new Tag();
t.setPageContext(pageContext);
t.setParent(null);
t.setXXX(...);
...
t.doStartTag();
...

etc.
843833
Thanks :-)
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 9 2001
Added on Oct 11 2001
3 comments
911 views