0

I have a problem when I try Override the getAsObject function

@FacesConverter("countryConverter")
public class CountryConverter implements Converter {

@EJB private CountryService countryService;

     @Override
        public Object getAsObject(FacesContext context, UIComponent arg1, String arg2){

         Country country = countryService.findByName(arg2);
         return country;
    }

}

SO when I load my combobox in every post back

<p:selectOneMenu value="#{registerController.selectedCountry}"
                converter="countryConverter">
                <f:selectItems value="#{registerController.countries}" var="country"
                    itemLabel="#{country.name}" itemValue="#{country}" />
            </p:selectOneMenu>

I got a nullPointer Exception in the findbyName line!, I check the arg2 paramater and it carrys a valid value, Also I test the service function and also it works fine... Outside the CountryConverter class.

The error that I got is the follow, as I described in the getAsObject function line

SEVERE: java.lang.NullPointerException
    at com.garagesalelive.controllers.converter.CountryConverter.getAsObject(CountryConverter.java:29)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:58)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:378)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

SEVERE:     at com.garagesalelive.controllers.converter.CountryConverter.getAsObject(CountryConverter.java:29)
SEVERE:     at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
SEVERE:     at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
SEVERE:     at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
SEVERE:     at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:58)
SEVERE:     at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
SEVERE:     at javax.faces.component.UIInput.validate(UIInput.java:960)
SEVERE:     at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
SEVERE:     at javax.faces.component.UIInput.processValidators(UIInput.java:698)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:378)
SEVERE:     at javax.faces.component.UIForm.processValidators(UIForm.java:253)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE:     at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
SEVERE:     at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
SEVERE:     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
SEVERE:     at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
SEVERE:     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
SEVERE:     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
SEVERE:     at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
SEVERE:     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
SEVERE:     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
SEVERE:     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE:     at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE:     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE:     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE:     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE:     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE:     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE:     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE:     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE:     at java.lang.Thread.run(Thread.java:722)
Avinash Singh
  • 2,564
  • 1
  • 14
  • 20
  • Add a setter for `CountryService countryService` attribute otherwise it cannot be injected. – Luiggi Mendoza Mar 05 '13 at 22:21
  • I don't think setter method is required for EJB annotation .What kind of EJB is that ? If it is local and running within same app then it should work.Can you post your EJB with annotations you are using. – Avinash Singh Mar 06 '13 at 00:34
  • Local public interface CountryService extends dao{} Stateless public class CountryServiceImpl implements CountryService {} public interface CountryDao extends dao {} Stateless public class CountryDaoImpl implements CountryDao{} ======== Note "@" symbol is suppose to be before each EJB reserve word, but stack does not allow me cuze of his mention user syntax – David Agustin Almanza Gaitan Mar 06 '13 at 15:35
  • possible duplicate of [JSF2, can I use @EJB to inject a service into a @FacesConverter?](http://stackoverflow.com/questions/7665673/jsf2-can-i-use-ejb-to-inject-a-service-into-a-facesconverter) – BalusC Mar 13 '13 at 17:06

3 Answers3

2

I think your countryService is null. May be it is an injection problem.

Try this

@ManagedBean
@RequestScoped
@FacesConverter("countryConverter")
public class CountryConverter implements Converter {...
  • Making the converter a managed bean won't resolve the problem. Also, it would be better to set it as `@ApplicationScoped` in order to have only once for all the application instead of creating a converter for every request. – Luiggi Mendoza Mar 05 '13 at 22:23
  • @LuiggiMendoza, actually it will. Per the JSF 2.0 Spec, only scoped beans are eligible targets for injection. Converters are not and that is why OP's injection is failing. I myself have used (and recommended) this trick successfully. Even if you search SO here, I'm sure you'll find answers with this in it. You're right on the `@RequestScoped` bit. It's unnecessary ,the `@ManagedBean` is sufficient for the purposes intended. Kerem Can Kurtay should also get rid of the converter naming and place the name on the managed bean annotation instead. It's this name that will be used in the markup – kolossus Mar 06 '13 at 04:23
  • @kolossus well then, the managed bean will be `@NoneScoped` so the bean would be created every time you call the `countryConverter` in JSF code. Thanks for the lesson, but I still think it should be `@ApplicationScoped` in order to have a single instance and not give extra job to GC. – Luiggi Mendoza Mar 06 '13 at 04:34
  • @LuiggiMendoza, the concern of wastefulness is very valid, but `@ApplicationScoped` is not going to be thread-safe. Quite frankly dont see an alternative – kolossus Mar 06 '13 at 04:35
  • @kolossus I assume this would be a `@Stateless` EJB and a `getData(long id)` operation should have no thread problems. – Luiggi Mendoza Mar 06 '13 at 04:41
  • It seems @LuiggiMendoza is right in one thing, none Scpoped Beans are not target for injections, but I alredy make my Converter implementation ManagedBean and Scoped Bean both request and Application, and it always fail it the countryService function – David Agustin Almanza Gaitan Mar 06 '13 at 15:43
  • 1
    This makes indeed no utter sense. You effectively end up with a converter which can be referenced in 2 completely different and independent ways. You should remove `@FacesConverter` to remove confusion. Otherwise it would still not work when you use `converter="countryConverter"` instead of `converter="#{countryConverter}"`. – BalusC Mar 13 '13 at 17:02
1

@EJB will not work on beans not managed by container. As per my understanding it works only with JSF Managed Beans , other EJB's and servlets.

If you are also using CDI library then you can try ,

@Inject private CountryService countryService;

or otherwise do a lookup,

    InitialContext ctx = new InitialContext();
    CountryService  countryService = 
(CountryService)ctx.lookup("ejb/countryService");

Your JNDI name will depend on your container or will be value of mappedBy attribute in your EJB if that is supported by your container and is specified by you on EJB.

Avinash Singh
  • 2,564
  • 1
  • 14
  • 20
-2

by calling ManagedBean through FacesContext:

 context.getApplication().getELResolver().getValue(context.getELContext(), null, "nameOfManagedBean");
Tamil Selvan C
  • 18,342
  • 12
  • 44
  • 63