Maybe use BeanProvider of deltaspike Le 19 nov. 2012 20:50, "Howard W. Smith, Jr." <smithh032...@gmail.com> a écrit :
> Hmmm... after thinking about it, I remembered to use @Inject on the bean > which I want to inject. So, I did the following: > > @FacesConverter(forClass = AddressType.class) > public static class AddressTypeControllerConverter implements Converter > { > > @Inject > private pf_AddressTypeController controller; > > public Object getAsObject(FacesContext facesContext, UIComponent > component, String value) { > if (value == null || value.length() == 0) { > return null; > } > return controller.ejbFacade.find(getKey(value)); > } > > but 'controller' is still not being injected. I even tried the same for the > actual 'ejbFacade' (which is a stateless bean), but that, too, resulted in > a NullPointerException, which tells me that @Inject is not injecting > pf_AddressTypeController (@sessionscoped bean) OR ejbFacade (@Stateless > bean instance of AddressTypeFacade). > > > Is there a general practice used for injecting beans in Converters.. > > > > On Mon, Nov 19, 2012 at 2:38 PM, Romain Manni-Bucau > <rmannibu...@gmail.com>wrote: > > > Can you reproduce it in a sample? What is on the npe line? > > Le 19 nov. 2012 20:25, "Howard W. Smith, Jr." <smithh032...@gmail.com> a > > écrit : > > > > > I am still migrating my app from JSF Managed beans to CDI managed > beans, > > so > > > I am (regression) testing, and I came across the exception below when > > > attempting to add a record into the database on one of the xhtml pages. > > > > > > Caused by: java.lang.NullPointerException > > > at > > > > > > jsf.address.pf_AddressTypeController$AddressTypeControllerConverter.getAsObject(pf_AddressTypeController.java:283) > > > > > > > > > The exception is caused by this line below. *ejbFacade* is a stateless > > bean > > > injected by @Inject. > > > > > > return controller.*ejbFacade*.find(getKey(value)); > > > > > > > > > The code below is as-is using JSF Managed Beans. Please advise on how I > > > should update the following code, so it will work well with CDI. > > > > > > @FacesConverter(forClass = AddressType.class) > > > public static class AddressTypeControllerConverter implements > > Converter > > > { > > > > > > public Object getAsObject(FacesContext facesContext, > UIComponent > > > component, String value) { > > > if (value == null || value.length() == 0) { > > > return null; > > > } > > > pf_AddressTypeController controller = > > > (pf_AddressTypeController) > facesContext.getApplication().getELResolver(). > > > getValue(facesContext.getELContext(), null, > > > "pf_addressTypeController"); > > > return controller.ejbFacade.find(getKey(value)); > > > } > > > > > > java.lang.Integer getKey(String value) { > > > java.lang.Integer key; > > > key = Integer.valueOf(value); > > > return key; > > > } > > > > > > String getStringKey(java.lang.Integer value) { > > > StringBuffer sb = new StringBuffer(); > > > sb.append(value); > > > return sb.toString(); > > > } > > > > > > public String getAsString(FacesContext facesContext, > UIComponent > > > component, Object object) { > > > if (object == null) { > > > return null; > > > } > > > if (object instanceof AddressType) { > > > AddressType o = (AddressType) object; > > > return getStringKey(o.getAddressTypeId()); > > > } else { > > > throw new IllegalArgumentException("object " + object > + " > > > is of type " + object.getClass().getName() + "; expected type: " + > > > pf_AddressTypeController.class.getName()); > > > } > > > } > > > } > > > } > > > > > >