1

I tried to get external context from FacesContext as followed and got NullPointerException:

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

What could possibly cause the problem?

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Best
  • 2,200
  • 9
  • 30
  • 49

1 Answers1

12

It can only be caused if FacesContext#getCurrentInstance() returned null. Any attempt to access or invoke a null reference will result in NullPointerException. See also its javadoc:

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

That FacesContext#getCurrentInstance() returns null can in turn only be caused if that line of code is not been executed inside the JSF context, i.e. when the code is not running during a HTTP request which is been served by the FacesServlet, who's the one responsible for creating the FacesContext as ThreadLocal. For example, inside a plain servlet, servlet filter or servlet listener or any other code which isn't executed during a HTTP request which runs the FacesServlet.

How to solve it properly depends on the functional requirement which isn't clear from the question. Generally, you'd either make sure that the HTTP request runs through the FacesServlet, or to access the information you're looking for by alternate means which is more appropriate for the context the code is currently running in.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks very much for your highly detail account:). Since thats all the lines of code I implemented(Related to jsf), I suppose that I need to get HTTP request up running. Would you be able to suggest me the simplest way to do it? – Best Nov 28 '11 at 19:14
  • 1
    As said, that really depends on the context where this line of code is running and the functional requirement. Where exactly is that line of code running? What exactly would you like to do for which you thought that the `ExternalContext` would be suitable? The `ExternalContext` is merely a facade around `ServletContext`, `HttpServletRequest`, `HttpSession` and `HttpServletResponse`. If either of them is available in the context where your code is running, just use one of them directly. Feel free to ask a new question wherein you describe the functional requirement in detail. – BalusC Nov 28 '11 at 19:16
  • Basically, I need to be able to transfer a user to from my screen to another tool in a different screen using ExternalContext#redirect. – Best Nov 28 '11 at 19:23
  • Again, what's the context where the code is currently running in? Is it for example inside `doFilter()` method of a servlet filter? If so, cast the provided `ServletResponse` argument to `HttpServletResponse` and use its `sendRedirect()` method. If you really insist doing it the JSF-ish way check [this](http://stackoverflow.com/questions/6384327/validation-before-rendering-page/6386785#6386785) or [this](http://stackoverflow.com/questions/7294651/is-there-any-easy-way-to-preprocess-and-redirect-get-requests/7294707#7294707) answer – BalusC Nov 28 '11 at 19:26