Reputation: 14731
I have a Converter
class where I have used the following so that I can use @Inject
to access my service class.
@Named("myMB")
@ViewAccessScoped
However when I tried to use
myservice.getCategories();
I am getting null pointer exception
at this line. What could be the reason for this?
I have used the same service method in ManagedBean
to populate selectOneMenu
, but when
used in Converter class, gives me exception.
Converter class
@FacesConverter("categoryConverter")
@Named("myMB")
@ViewAccessScoped
public class CategoryConverter implements Converter {
@Inject
CategoryService myservice;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component,
String value) {
System.out.println("reached in converter "+value);
try {
List<Category> cat = myservice.getCategories();
for (Category cat : category) {
if (cat.getCategoryCode() == value) {
return cat;
}
}
}
} catch (Exception e) {
System.out.println("exception from getAsObject ");
e.printStackTrace();
}
return null;
}
Upvotes: 0
Views: 893
Reputation: 1108842
That can happen if you used it as @FacesConverter
instance instead of as @Named
instance. The @Inject
doesn't work on @FacesConverter
. Get rid of @FacesConverter
to avoid future confusion and reference the converter as converter="#{categoryConverter}"
(which uses @Named
) instead of as converter="categoryConverter"
(which uses @FacesConverter
).
Note that I assume that the Spring part is properly configured, otherwise it would be still null
. I don't do Spring, so I can't tell form top of head if it works inside a CDI managed bean instead of a Spring managed bean. I find it only surprising and amusing that you're mixing CDI and Spring, while Spring is intented as competitor/alternative to CDI/EJB.
Upvotes: 3