3

I am using Spring, Hibernate and JSF.

In order to get a bean from application context I write:

public static Object findBean(String name) {
  return FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean(name);
 }

However, the FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null, so calling getBean() on it throws NullPointerException

Do I need to define anything anywhere?

EDITED

I need to get the bean from application context and not jsf bean

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Dejell
  • 13,947
  • 40
  • 146
  • 229
  • 3
    What reference exactly is `null`? Those are static methods, they can impossibly throw NPE. Don't you mean that `FacesContext#getCurrentInstance()` returned `null` and that the NPE is actually been thrown inside `getWebApplicationContext()` method? – BalusC Jan 24 '11 at 19:32
  • FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null. The FacesContext.getCurrentInstance() is not null! – Dejell Jan 24 '11 at 19:38

1 Answers1

6

FacesContext.getCurrentInstance() returns null if your are not within a request that is handled by the FacesServlet. Make sure you are handling a request passed through that servlet.

Update: If the faces context is not null, the reason why the application context may be null is that there is no servlet context attribute named org.springframework.web.context.WebApplicationContext.ROOT. This means spring is not initialized properly. Make sure you have either the spring ContextLoaderListener mapped in web.xml, or the DispatcherServlet

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140