nullpointer using response.encodeURL

From: kevin (kevin_at_mtel.co.uk)
Date: 06/24/04


Date: 24 Jun 2004 03:50:27 -0700

I have an intermittent problem with bean functions crashing trying to
use the
ServletResponse.encodeURL() method. This is traceable to the
ServletResponse object being null when the method is called.
This bug only seems to occur on the use of
servletResponse.encodeURL(string), and then seemingly randomly. The
bean functions also use sendRedirect() and encodeRedirectURL() fairly
frequently.

There are other beans in the app and they relate to specific use
pages, which communicate with each other by session attributes set up
by the main bean as references. So the menu bean on initialisation
reads the attribute "parent" from the session and gets its reference
to the main bean from it. Similary for the other bean communication,
and I do not seem to have any problem reading references to functions
and data from the session from other beans.

The object is set up for the bean by the initialise function being
called from the jsp page as follows in each of the pages accessing the
bean data and functions (they are all session scope)

<%@ page errorPage="errorframe.jsp" %>
<jsp:useBean id="fisWeb" scope="session"
class="uk.co.mtel.website.FisWeb">
<% fisWeb.initialise(pageContext); %></jsp:useBean>
<% fisWeb.setPageContext(pageContext); %>

the bean function initialise:-

    /**
     * Initialise Bean
     */
    public void initialise(javax.servlet.jsp.PageContext p ) {

        /* initialise the FisBean */
        super.initialise(p);
        session = p.getSession();
        errorMessage=new StringBuffer("Please Login: ");

        return;
    }

the ancestor bean has the common code :-

    protected HttpServletRequest servletRequest = null;
    protected HttpServletResponse servletResponse = null;
    protected PageContext pageContext = null;
    protected ServletContext servletContext = null;

    /**
     * Initialise Bean
     */
    public void initialise(javax.servlet.jsp.PageContext p ) {

        /* extract some useful objects from the JSP page */
        ServletConfig conf = p.getServletConfig();
        ServletContext context = p.getServletContext();

        /* Load the Postgresql JDBC driver */
        try{
            Class.forName("org.postgresql.Driver");
            initialised=true;
        }
        catch (ClassNotFoundException cnfe){
            String err = "Unable to load PostgreSQL JDBC drivers:
"+cnfe.toString();
            context.log(err);
            throw( new RuntimeException(err));
        }
        return;
    }

the code which occasionally throws nullpointer

    public String getNews() {
        StringBuffer sb = new StringBuffer();
        ResultSet rst;
        String strLastDesc="";
        String strMessage=null;
        String strHeading=null;
        message msg=new message(strMenuGroup, name);
        msg.setUsertype(strUserType);
        
        try {
            rst = msg.myMessages(livedb);
        
            while (rst.next()){
                strHeading=rst.getString(1);
                strMessage=rst.getString(2);
                if (! strLastDesc.equals(strHeading)){
                     strLastDesc = strHeading;

                     // *** random-ish failure here ***
                     sb.append("<TR><TD bgColor=\"#99cccc\"
align=\"left\">" + servletResponse.encodeURL(strHeading) +
"</TD></TR>");
                }
                sb.append("<TR><TD align=\"left\">" +
servletResponse.encodeURL(strMessage) + "</TD></TR>");
            }
            rst.close();
            rst=null;
        }
        catch (SQLException sqle) {
            fatal("Internal error","getNews() " + sqle.getMessage());
        }
        return sb.toString();
    }



Relevant Pages

  • Returning object created in bean onto a page
    ... intended to have a session scope. ... The user id and pwd are passed to the jsp page from login.html ... 2.The jsp page contains a reference to the object theUser, ... There is another bean on the page, log, an object of login class. ...
    (comp.lang.java.programmer)
  • Re: JBoss und MDBs
    ... Er meint die JMS Api Spezifikation. ... Nach meinem Verständnis sollte die Session ... > Bean die Message absetzen und weitermachen mit was auch immer aus ihrer ...
    (de.comp.lang.java)
  • Re: JBoss und MDBs
    ... >> Session Bean zum Erliegen. ... Am anderen Ende der Queue sitzt die MDB. ... Next by Date: ...
    (de.comp.lang.java)
  • Re: JSP - saving textfield values before loading intermediate page
    ... > present into the fields of the bean. ... a request is needed to get the session. ... servlet that takes care of it. ...
    (comp.lang.java.programmer)
  • [EJB] Long lasting session bean - how to implement progress indicator?
    ... progress indicator in client that is calling time consuming session ... I have one stateful session bean that imports data from one database ... My client is a Swing application launched inside Application Client ...
    (comp.lang.java.programmer)