users@glassfish.java.net

Re: Random NullPointerExceptions from ValueExpression

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Mon, 20 Aug 2007 16:11:40 -0700

Woods, Michael G wrote:
>
> I still haven’t found a solution to this problem thus far. In my
> testing I find that the Servlet returns a valid response the first two
> times then getValue() throws a NullPointerException eight times, then
> two valid results, then eight failures, etc etc…
>
> This is constantly reproducible. I’ve also tried containing the
> following code in a do {…} while (tc == null); however that leads to
> an infinite loop. While debugging and stepping through the code, all
> the variables have values (except tc) and I can’t spot anything much
> out of the norm.
>
Can you create and attached a test case?
>
> FacesContext fc = this.getFacesContext(request, response);
>
> if (null != fc) {
>
> ExpressionFactory expFactory = fc.getApplication().getExpressionFactory();
>
> ELContext elc = fc.getELContext();
>
> ValueExpression valExp = expFactory.createValueExpression(elc,
> "#{taskController}", TaskController.class);
>
> try {
>
> tc = (TaskController) valExp.getValue(elc); //This is where the code fails
>
> …
>
> …
>
> …
>
> } catch (NullPointerException e) {
>
> log.severe("***** ValueExpression.getValue(taskController) failed
> again...");
>
> response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
> "valExp.getValue() failed");
>
> }
>
> } else {
>
> response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
> "FacesContext was null");
>
> }
>
> Anyone have any ideas?
>
> Thanks,
> Mike Woods
>
> ------------------------------------------------------------------------
>
> *From:* Woods, Michael G
> *Sent:* Thursday, August 16, 2007 12:01 PM
> *To:* users_at_glassfish.dev.java.net
> *Subject:* Random NullPointerExceptions from ValueExpression
>
> I have a problem grabbing “TaskController” from my application. Upon
> logging into the web app and requesting the servlet that the following
> code is running, I randomly get the proper output or a
> NullPointerException. The exception occurs in the 5^th line of code
> listed below:
>
> FacesContext fc = FacesContext.getCurrentInstance();
>
> Application app = fc.getApplication();
>
> ExpressionFactory expFactory = app.getExpressionFactory();
>
> ValueExpression valExp =
> expFactory.createValueExpression(fc.getELContext(),
> "#{taskController}", TaskController.class);
>
> TaskController tc = (TaskController) valExp.getValue(fc.getELContext());
>
> tc is null after this, however if I request the link again and again,
> I’ll get proper output about once every five requests (the very first
> request is always correct).
>
> I’ve also tried the deprecated ValueBinding method however that
> results in the same situation. Anyone have a suggestion on where to
> look for the cause of this problem?
>
> Thanks,
>
> Mike Woods
>