java.lang.NullPointerException selectOneMenu convertor - jsf

I have a class Specialization
public class Specialization implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#NotNull
#Column(name = "id")
private Integer id;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 45)
#Column(name = "name")
private String name;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "specialization")
private Collection<Doctor> doctorCollection;
In my webpage i have a selectOneMenu
<h:selectOneMenu value="#{specializationMB.specialization}"
converter="#{specializationConverter}">
<f:selectItems value="#{specializationMB.allSpecializations}" var="specialization"
itemLabel="#{specialization.name}" itemValue="#{specialization}" />
</h:selectOneMenu>
GenericDao where is the find method
public abstract class GenericDao<T> {
#PersistenceContext
private EntityManager em;
private Class<T> entityClass;
public GenericDao(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void create(T entity) {
em.persist(entity);
}
public void delete(T entity) {
T toRemoved = em.merge(entity);
em.remove(toRemoved);
}
public T update(T entity) {
return em.merge(entity);
}
public T find(int entityID) {
return em.find(entityClass, entityID);
}
#SuppressWarnings({"unchecked", "rawtypes"})
public List<T> findAll() {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return em.createQuery(cq).getResultList();
}
}
I create the converter for my class Specialization
public class SpecializationConverter implements Converter{
private static SpecializationDao sdao = new SpecializationDao();
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return sdao.find(new Integer(value));
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return ((Specialization) value).getId().toString();
}
}
But I get an error with my selectOneMenu, when i go to the next page.
java.lang.NullPointerException
at pl.ePrzychodnia.dao.GenericDao.find(GenericDao.java:40)
at pl.ePrzychodnia.converter.SpecializationConverter.getAsObject(SpecializationConverter.java:24)
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 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.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:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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)
I don't now why. Can anyone help?

private static SpecializationDao sdao = new SpecializationDao();
Assuming this DAO is a subclass of your GenericDAO, can you tell me when is the EntityManager injected? Let the container create your DAO! Your EntityManager is not injected, therefore you get the NPE.
I would create a stateless session bean (if you work with EJB) and then inject it into the converter.
#Stateless
public class SpecializationService extends GenericCRUDService {...}
public class SpecializationConverter implements Converter{
#Inject
private SpecializationService specializationService;
...
I also think that the term and pattern DAO is a bit outdated. Read about it for example here.

Related

'ExceptionHandlerFactory was not configured properly' on Glassfish 3.1.2.2

I'm trying to handle uncaught exceptions through ExceptionHandler. Following the code in JSF2 complete reference, I've created the classes for my handler. But when I deploy my app, it throws the next stackTrace:
SEVERE: Critical error during deployment:
com.sun.faces.config.ConfigurationException: Factory 'javax.faces.context.ExceptionHandlerFactory' was not configured properly.
at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:305)
at com.sun.faces.config.processor.FactoryConfigProcessor.process(FactoryConfigProcessor.java:219)
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:360)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:225)
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:4750)
at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:550)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5366)
at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2019)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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:662)
Caused by: javax.faces.FacesException: exception.ExceptionHandlerFactory
at javax.faces.FactoryFinder.getImplGivenPreviousImpl(FactoryFinder.java:643)
at javax.faces.FactoryFinder.getImplementationInstance(FactoryFinder.java:509)
at javax.faces.FactoryFinder.access$400(FactoryFinder.java:139)
at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:993)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:343)
at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:303)
... 45 more
Caused by: java.lang.InstantiationException: exception.ExceptionHandlerFactory
at java.lang.Class.newInstance0(Class.java:342)
at java.lang.Class.newInstance(Class.java:310)
at javax.faces.FactoryFinder.getImplGivenPreviousImpl(FactoryFinder.java:641)
... 50 more
This is my faces-config.xml:
<factory>
<exception-handler-factory>
exception.ExceptionHandlerFactory
</exception-handler-factory>
</factory>
ExceptionHandlerFactory.java
public class ExceptionHandlerFactory extends javax.faces.context.ExceptionHandlerFactory {
private transient Logger logger = Logger.getLogger(this.getClass().getName());
private ExceptionHandlerFactory parent;
public ExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
#Override
public ExceptionHandler getExceptionHandler() {
ExceptionHandler result = parent.getExceptionHandler();
result = new CustomExceptionHandler(result);
return result;
}
}
CustomExceptionHandler.java
public class CustomExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler parent;
public CustomExceptionHandler(ExceptionHandler parent) {
this.parent = parent;
}
#Override
public ExceptionHandler getWrapped() {
return this.parent;
}
#Override
public void handle() throws FacesException {
for (Iterator<ExceptionQueuedEvent> i =
getUnhandledExceptionQueuedEvents().iterator();
i.hasNext();) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context =
(ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();
if (t instanceof ViewExpiredException) {
ViewExpiredException vee = (ViewExpiredException) t;
FacesContext fc = FacesContext.getCurrentInstance();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
fc.getExternalContext().
getFlash().put("currentViewId",
vee.getViewId());
nav.handleNavigation(fc, null, "/login?faces-redirect=true");
fc.renderResponse();
} finally {
i.remove();
}
}
}
getWrapped().handle();
}
}
Server is Glassfish 3.1.2.2.
I need to to some logic when I get the ViewExpiredException. Did I miss something?
Thanks in advance !!!!
EDIT:
As #BalusC pointed, I changed my factory class name, so this is my CustomExceptionHandlerFactory.java:
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
#Override
public ExceptionHandler getExceptionHandler() {
ExceptionHandler result = parent.getExceptionHandler();
result = new CustomExceptionHandler(result);
return result;
}
}
and my faces-config.xml:
<factory>
<exception-handler-factory>
com.exception.CustomExceptionHandlerFactory
</exception-handler-factory>
</factory>
I hope this could be helpful for anyone with this silly problem.
Your mistake is that you reused the same classname as an existing API class for some unclear reason and got them mixed up in the code.
The parent constructor argument (and local variable) has to be of type javax.faces.context.ExceptionHandlerFactory, not of your exception.ExceptionHandlerFactory. This wrong type caused that JSF was unable to invoke the constructor and hence the -rather self explaining- InstantiationException (it couldn't be instantiated).
Better rename your class to something unique like CustomExceptionHandlerFactory.

JSF 2: Can't load object in selectOneMenu

I'm trying to fill a dropdown menu, with some objects that I retrieve from database.
So, after reading this topic I did this:
<h:form id="register_form">
<h:panelGrid columns="3">
<h:outputLabel for="client" value="Client:" />
<h:selectOneMenu id="client" value="#{reuniaoc.cliente}" converter="#{clienteConverter}" >
<f:selectItems value="#{reuniaoc.clientes}" var="_cliente" itemValue="#{_cliente}" itemLabel="#{_cliente.id}" />
<f:ajax event="blur" render="m_client" />
</h:selectOneMenu>
<h:message id="m_client" for="client" />
</h:panelGrid>
</h:form>
This is my controller :
package control;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import model.Cliente;
import utility.Message;
import bean.ReuniaoBean;
import eao.ClienteEAO;
import eao.ReuniaoEAO;
#ManagedBean(name="reuniaoc")
#RequestScoped
public class ReuniaoController implements Serializable {
private static final long serialVersionUID = 7737795872305798041L;
#EJB ReuniaoEAO rEAO;
#EJB ClienteEAO cEAO;
private ReuniaoBean rb;
private Cliente cliente;
private List<Cliente> clientes;
#PostConstruct
public void init() {
clientes = cEAO.list();
}
private void addMessage(Message message){
FacesMessage fMessage = new FacesMessage(message.getMessage());
FacesContext.getCurrentInstance().addMessage(null, fMessage);
}
public void register(){
Message message = rEAO.create(rb.getReuniao());
addMessage(message);
}
public String getClientes(){
for (Cliente c : clientes)
System.out.println(c.getFantasia());
return "/funcionario/register/reuniao.xhtml?faces-redirect=true";
}
// get's and set's
public ReuniaoBean getRb() {
return rb;
}
public void setRb(ReuniaoBean rb) {
this.rb = rb;
}
public void setClientes(List<Cliente> clientes) {
this.clientes = clientes;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
}
And my converter:
package bean.converter;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import model.Cliente;
import eao.ClienteEAO;
#ManagedBean
#RequestScoped
public class ClienteConverter implements Converter {
#EJB private ClienteEAO clienteEAO;
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Cliente) || ((Cliente) value).getId() == null) {
return null;
}
return String.valueOf(((Cliente) value).getId());
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || !value.matches("\\d+")) {
return null;
}
Cliente c = clienteEAO.find(Integer.valueOf(value));
if (c == null) {
throw new ConverterException(new FacesMessage("Unknown operation ID: " + value));
}
return c;
}
}
But when I try to load the page, gives me this error: (sorry for the exception be so long)
java.lang.IllegalArgumentException
at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:216)
at com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:762)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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:662)
For me it seems very consistent my code, but there's something I'm missing here.
Any idea ?
public String getClientes(){
for (Cliente c : clientes)
System.out.println(c.getFantasia());
return "/funcionario/register/reuniao.xhtml?faces-redirect=true";
}
This <f:selectItems value> getter doesn't return the list of clients. It returns a navigation case string, which isn't recognized as a valid value argument of <f:selectItems>. It look like that you're mixing action methods with property getters.
Fix the getter accordingly to be a real getter:
public List<Cliente> getClientes() {
return clientes;
}
The setter method on that property is by the way unnecessary. It won't be used in any way in this construct.
See also:
Our h:selectOneMenu wiki page

p:selectonemenu produces an extra null option and converter fails

I have a problem with p:selectonemenu. Server Logs and my codes are below.
The problem: I try to code a simple application with CRUD functionality.
h:selectonemenu works as expected. But when I switch to p:selectonemenu I get converter exception.
Normally my options data includes 3 rows in database. I made options printed after initilization as debug purpose, my list is initilized with there elements as normal.
However with p:selectmenu, my list is initialized with there elements as again (proved by syso in the init function.), but an extra selectonemenu option,null one reaches to converter. And it fires exception.
I debugged the application and saw the extra null object there too. My converter is called 3 times with h:selectonemenu, at least 4 times with p:selectmenu.
I could have made a mistake. But is this my mistake or what else?
Glassfish 3.1.2, PM 3.3.1
Server Log (h:selectmenu):
INFO: Hibernate:
select
grup0_.grup_id as grup1_8_,
grup0_.grup_adi as grup2_8_
from
Grup grup0_
INFO: Selectonemenu Option#Init :Test Value 1
INFO: Selectonemenu Option#Init :Test Value 2
INFO: Selectonemenu Option#Init :Test Value 3
INFO: Selectonemenu Option#Converter :Test Value 1
INFO: Selectonemenu Option#Converter :Test Value 2
INFO: Selectonemenu Option#Converter :Test Value 3
Server Log (p:selectmenu):
INFO: Hibernate:
select
grup0_.grup_id as grup1_8_,
grup0_.grup_adi as grup2_8_
from
Grup grup0_
INFO: Selectonemenu Option#Init :Test Value 1
INFO: Selectonemenu Option#Init :Test Value 2
INFO: Selectonemenu Option#Init :Test Value 3
INFO: Selectonemenu Option#Converter :Test Value 1
INFO: Selectonemenu Option#Converter :Test Value 2
INFO: Selectonemenu Option#Converter :Test Value 3
INFO: Hibernate:
select
grup0_.grup_id as grup1_8_,
grup0_.grup_adi as grup2_8_
from
Grup grup0_
INFO: Selectonemenu Option#Init :Test Value 1
INFO: Selectonemenu Option#Init :Test Value 2
INFO: Selectonemenu Option#Init :Test Value 3
INFO: Selectonemenu Option#Converter :Test Value 1
INFO: Selectonemenu Option#Converter :Test Value 2
INFO: Selectonemenu Option#Converter :Test Value 3
**INFO: Selectonemenu Option#Converter :null**
SEVERE: Error Rendering View[/NewFile.xhtml]
javax.faces.convert.ConverterException: getAsString Error
at converters.GrupConverter.getAsString(GrupConverter.java:60)
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:76)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeLabel(SelectOneMenuRenderer.java:120)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:89)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:62)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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)
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.convert.ConverterException: getAsString Error
at converters.GrupConverter.getAsString(GrupConverter.java:60)
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:76)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeLabel(SelectOneMenuRenderer.java:120)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:89)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:62)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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)
test.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<body>
<h:form>
<p:selectOneMenu id="grup" value="#{grupBean.grup}" converter="grupConverter">
<f:selectItems value="#{grupBean.grupList}" var="grp" itemLabel="#{grp.grup_adi}" itemValue="#{grp}" />
</p:selectOneMenu>
</h:form>
</body>
</html>
Grup.java
package model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
#Entity
public class Grup implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8730335332086520080L;
#Id
#GeneratedValue
private Integer grup_id;
private String grup_adi;
#ManyToMany(mappedBy="gruplar")
private List<Kullanici> kullanicilar = new ArrayList<Kullanici>();
public Integer getGrup_id() {
return grup_id;
}
public void setGrup_id(Integer grup_id) {
this.grup_id = grup_id;
}
public String getGrup_adi() {
return grup_adi;
}
public void setGrup_adi(String grup_adi) {
this.grup_adi = grup_adi;
}
public List<Kullanici> getKullanicilar() {
return kullanicilar;
}
public void setKullanicilar(List<Kullanici> kullanicilar) {
this.kullanicilar = kullanicilar;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((grup_id == null) ? 0 : grup_id.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Grup))
return false;
Grup other = (Grup) obj;
if (grup_id == null) {
if (other.grup_id != null)
return false;
} else if (!grup_id.equals(other.grup_id))
return false;
return true;
}
}
GrupConverter.java
package converters;
import model.Grup;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import util.EntityUtil;
#FacesConverter(value = "grupConverter")
public class GrupConverter implements Converter {
EntityManager em = EntityUtil.getEntityManager();
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String deger) {
Grup findGrup = null;
if (deger == null || deger.isEmpty()) {
return null;
}
try {
findGrup = em.find(Grup.class, Integer.parseInt(deger));
// System.out.println(findGrup.);
} catch (Exception e) {
throw new ConverterException(new FacesMessage(String.format(
"Cannot convert %s to Grup", deger)), e);
}
return findGrup;
}
#Override
public String getAsString(FacesContext context, UIComponent component,
Object deger) {
String val = null;
// ....................
Grup d = (Grup) deger;
System.out
.println("Selectonemenu Option#Converter :" + d.getGrup_adi());
// .....................
try {
Grup b = (Grup) deger;
val = Integer.toString(b.getGrup_id());
} catch (Exception e) {
throw new ConverterException(new FacesMessage("getAsString Error"));
}
return val;
}
}
GrupBean.java
package beans;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import model.Birim;
import model.Grup;
import util.EntityUtil;
#ManagedBean
#ViewScoped
public class GrupBean implements Serializable{
private static final long serialVersionUID = 2166603645975073743L;
List<Grup> grupList=null;
List<Birim> birimList;
EntityManager em = EntityUtil.getEntityManager();
private boolean edit;
Grup grup=new Grup();
#PostConstruct
public void init()
{
grupList = em.createQuery("from Grup").getResultList();
//birimList=em.createQuery("from Birim").getResultList();
for (Grup grup : grupList) {
System.out.println("Selectonemenu Option#Init :"+grup.getGrup_adi());
}
}
public void add()
{
System.out.println("Add");
em.getTransaction().begin();
em.persist(grup);
em.getTransaction().commit();
System.out.println("End of Add");
grupList = em.createQuery("from Grup").getResultList();
edit = false;
grup = new Grup(); // Reset placeholder.
}
public void edit(Grup grup) {
this.grup = grup;
//System.out.println("Grup Edit"+grup.anAdi());
edit = true;
}
public void save() {
// dao.update(item);
System.out.println("save");
em.getTransaction().begin();
em.merge(grup);
em.getTransaction().commit();
System.out.println("End of save");
edit = false;
grupList = em.createQuery("from Grup").getResultList();
grup = new Grup(); // Reset placeholder.
}
public void delete(Grup grup) {
System.out.println("delete");
em.getTransaction().begin();
em.remove(grup);
em.getTransaction().commit();
System.out.println("End of delete");
grup = new Grup(); // Reset placeholder.
edit = false;
grupList = em.createQuery("from Grup").getResultList();
}
public List<Grup> getGrupList() {
return grupList;
}
public void setGrupList(List<Grup> grupList) {
this.grupList = grupList;
}
public Grup getGrup() {
return grup;
}
public void setGrup(Grup grup) {
this.grup = grup;
}
public boolean isEdit() {
return edit;
}
public List<Birim> getBirimList() {
return birimList;
}
public void setBirimList(List<Birim> birimList) {
this.birimList = birimList;
}
}

javax.el.PropertyNotFoundException: Target Unreachable, 'null' returned null on foreign key reference [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 7 years ago.
I know there is something wrong with the managedBean but i cant figure out what it is
When I try departmentManagedBean.department.faculty.facultyName from the jsf page it gives a null pointer like this:
javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml #30,260 value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null
javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml #30,260 value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.richfaces.component.util.InputUtils.findConverter(InputUtils.java:123)
at org.richfaces.component.util.InputUtils$1.getConverterByProperty(InputUtils.java:56)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:148)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:137)
at org.richfaces.renderkit.InputRendererBase.getConvertedValue(InputRendererBase.java:50)
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.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.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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:636)
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#NotNull
#Column(name = "DepartmentID")
private Integer departmentID;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 255)
#Column(name = "DepartmentName")
private String departmentName;
#JoinColumn(name = "FacultyName", referencedColumnName = "FacultyName")
#ManyToOne(optional = false, fetch = FetchType.EAGER)
public Faculty faculty;
public Faculty getFaculty() {
return faculty;
}
public void setFaculty(Faculty faculty) {
this.faculty = faculty;
}
public Department() {
}
public Department(Integer departmentID) {
this.departmentID = departmentID;
}
public Department(Integer departmentID, String departmentName) {
this.departmentID = departmentID;
this.departmentName = departmentName;
}
public Integer getDepartmentID() {
return departmentID;
}
public void setDepartmentID(Integer departmentID) {
this.departmentID = departmentID;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
public class DepartmentSessionBean {
#PersistenceContext(unitName = "ApplicationPU")
private EntityManager em;
public void persist(Department department) {
em.persist(department);
}
}
public class Faculty implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#NotNull
#Column(name = "FacultyID")
private Integer facultyID;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 255)
#Column(name = "FacultyName")
private String facultyName;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty", fetch = FetchType.EAGER)
private List<Department> departmentList;
public Faculty() {
}
public Faculty(Integer facultyID) {
this.facultyID = facultyID;
}
public Faculty(Integer facultyID, String facultyName) {
this.facultyID = facultyID;
this.facultyName = facultyName;
}
public Integer getFacultyID() {
return facultyID;
}
public void setFacultyID(Integer facultyID) {
this.facultyID = facultyID;
}
public String getFacultyName() {
return facultyName;
}
public void setFacultyName(String facultyName) {
this.facultyName = facultyName;
}
public List<Department> getDepartmentList() {
return departmentList;
}
public void setDepartmentList(List<Department> departmentList) {
this.departmentList = departmentList;
}
}
This is the ManagedBean that I need a solution with please:
#ViewScoped public class DepartmentRequestBean {
#EJB
private DepartmentSessionBean departmentSessionBean;
private Department department = new Department();
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public void save(){
departmentSessionBean.persist(department);
}
public DepartmentRequestBean() {
this.faculty = new Faculty();
}}
value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null
This basically means that one of the following values
#{departmentRequestBean.department}
#{departmentRequestBean.department.faculty}
returned null during processing of the form submit. As you've explicitly created the Department yourself, then it can only mean that Faculty is still null.
To fix it properly, replace in DepartmentRequestBean
private Department department = new Department();
by
private Department department;
#PostConstruct
public void init() {
department = new Department();
department.setFaculty(new Faculty());
}
initialize your department class in backing bean like this way
public Department getDepartment() {
if(department == null){
department = (Department)super.getInstance(Department.class);
}
return department;
}
this will create a new department object if the object become null

Changing requestscoped to sessionscoped gives an error

When I changed my managedbean from requestscoped to sessionscoped it gives this error when deploying the app. Can anyone explain what is happening?
Does the ejb injection fails in the postconstruct? Also when changing it back to requestscoped I get the same error. When restarting NetBeans the requestscoped works.
SEVERE: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5332)
at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2018)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
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:680)
Caused by: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2743)
at org.apache.catalina.core.StandardContext.addApplicationListener(StandardContext.java:1966)
at com.sun.enterprise.web.TomcatDeploymentConfig.configureApplicationListener(TomcatDeploymentConfig.java:235)
at com.sun.enterprise.web.TomcatDeploymentConfig.configureWebModule(TomcatDeploymentConfig.java:94)
at com.sun.enterprise.web.WebModuleContextConfig.start(WebModuleContextConfig.java:274)
at com.sun.enterprise.web.WebModuleContextConfig.lifecycleEvent(WebModuleContextConfig.java:172)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:149)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5329)
... 39 more
Caused by: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2853)
at org.apache.catalina.core.StandardContext.loadListener(StandardContext.java:4806)
at com.sun.enterprise.web.WebModule.loadListener(WebModule.java:1599)
at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2740)
... 46 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:315)
at com.sun.enterprise.web.WebContainer.createListenerInstance(WebContainer.java:761)
at com.sun.enterprise.web.WebModule.createListenerInstance(WebModule.java:1987)
at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2851)
... 49 more
Caused by: java.lang.NullPointerException
at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:477)
at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:420)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:299)
... 52 more
WARNING: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:921)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2018)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
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:680)
SEVERE: Exception while invoking class com.sun.enterprise.web.WebApplication start method
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.glassfish.webservices.WSServletContextListener
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:138)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
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:680)
The bean:
package Controllers;
import Domain.Device;
import Domain.Employee;
import Domain.Tags;
import Service.CompanyFacade;
import Service.DeviceFacade;
import Service.EmployeeFacade;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
#Named(value = "employeeController")
#RequestScoped
public class EmployeeController {
#Inject
private EmployeeFacade employeeFacade;
#Inject
private CompanyFacade companyFacade;
#Inject
private DeviceFacade deviceFacade;
private Employee selectedEmployee;
private String emptyMessage;
private Long employeeID;
private String firstNameOld;
private String firstNameNew;
private String insertionOld;
private String insertionNew;
private String lastNameOld;
private String lastNameNew;
private String functionNameNew;
private String functionNameOld;
private String emailNew;
private String emailOld;
private List<Employee> employees;
/**
* Creates a new instance of EmployeeController
*/
public EmployeeController() {
this.emptyMessage = "Er zijn geen werknemers gevonden.";
}
#PostConstruct
public void init(){
String user = FacesContext.getCurrentInstance()
.getExternalContext()
.getRemoteUser();
Long companyID = null;
System.out.println("user: " + user);
try{
companyID = this.companyFacade.getCompanyIDByUser(user);
}
catch(NullPointerException ex){
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Kan geen werknemers vinden.", ""));
}
System.out.println(companyID);
if (companyID == null) {
this.employees = new ArrayList<Employee>();
}else{
this.employees = (List<Employee>) this.employeeFacade.getAllByCompany(companyID);
}
}
public Collection<Employee> getEmployees(){
return this.employees;
}
public Collection<Tags> getTags(){
return this.employeeFacade.getTags(1L, 1L);
}
public Collection<Device> getDevices(){
return new ArrayList<Device>();
}
public String getEmptyMessage() {
return emptyMessage;
}
public void firstNameChanged(ValueChangeEvent event) {
System.out.println("firstNameChanged called.");
this.firstNameOld = event.getOldValue().toString();
this.firstNameNew = event.getNewValue().toString();
//FacesContext context = FacesContext.getCurrentInstance();
//Employee emp = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
this.employeeID = this.selectedEmployee.getId();
}
public void insertionChanged(ValueChangeEvent event){
this.insertionOld = event.getOldValue().toString();
this.insertionNew = event.getNewValue().toString();
// FacesContext context = FacesContext.getCurrentInstance();
// Employee emp = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
this.employeeID = this.selectedEmployee.getId();
}
public void lastNameChanged(ValueChangeEvent event){
this.lastNameOld = event.getOldValue().toString();
this.lastNameNew = event.getNewValue().toString();
// FacesContext context = FacesContext.getCurrentInstance();
// Employee emp = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
this.employeeID = this.selectedEmployee.getId();
}
public void emailChanged(ValueChangeEvent event){
this.emailOld = event.getOldValue().toString();
this.emailNew = event.getNewValue().toString();
// FacesContext context = FacesContext.getCurrentInstance();
// Employee emp = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
this.employeeID = this.selectedEmployee.getId();
}
public void functionNameChanged(ValueChangeEvent event){
this.functionNameOld = event.getOldValue().toString();
this.functionNameNew = event.getNewValue().toString();
// FacesContext context = FacesContext.getCurrentInstance();
// Employee emp = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
this.employeeID = this.selectedEmployee.getId();
}
public void saveName(){
System.out.println("saveName called.");
if(this.firstNameNew == null || this.firstNameOld == null){
}
else if(!this.firstNameNew.equals(this.firstNameOld)){
if (this.employeeID != null) {
Employee emp = this.employeeFacade.getByID(this.employeeID);
emp.setFirstName(this.firstNameNew);
this.employeeFacade.edit(emp);
this.employeeID = null;
this.firstNameNew = null;
this.firstNameOld = null;
}
}
}
public void saveInsertion(){
if(this.insertionNew == null || this.insertionOld == null){
}
else if(!this.insertionNew.equals(this.insertionOld)){
if (this.employeeID != null) {
Employee emp = this.employeeFacade.getByID(this.employeeID);
emp.setInsertion(this.insertionNew);
this.employeeFacade.edit(emp);
this.employeeID = null;
this.insertionNew = null;
this.insertionOld = null;
}
}
}
public void saveLastName(){
if(this.lastNameNew == null || this.lastNameOld == null){
}
else if(!this.lastNameNew.equals(this.lastNameOld)){
if (this.employeeID != null) {
Employee emp = this.employeeFacade.getByID(this.employeeID);
emp.setLastName(this.lastNameNew);
this.employeeFacade.edit(emp);
this.employeeID = null;
this.lastNameNew = null;
this.lastNameOld = null;
}
}
}
public void saveFunctionName(){
if(this.functionNameNew == null || this.functionNameOld == null){
}
else if(!this.functionNameNew.equals(this.functionNameOld)){
if (this.employeeID != null) {
Employee emp = this.employeeFacade.getByID(this.employeeID);
emp.setFunctionName(this.functionNameNew);
this.employeeFacade.edit(emp);
this.employeeID = null;
this.functionNameNew = null;
this.functionNameOld = null;
}
}
}
public void saveEmail(){
if(this.emailNew == null || this.emailOld == null){
}
else if(!this.emailNew.equals(this.emailOld)){
if (this.employeeID != null) {
Employee emp = this.employeeFacade.getByID(this.employeeID);
emp.setEmail(this.emailNew);
this.employeeFacade.edit(emp);
this.employeeID = null;
this.emailNew = null;
this.emailOld = null;
}
}
}
public void cancel(){
this.employeeID = null;
}
public Employee getSelectedEmployee() {
return selectedEmployee;
}
public void setSelectedEmployee(Employee selectedEmployee) {
System.out.println("setEmp: " + selectedEmployee.toString());
this.selectedEmployee = selectedEmployee;
}
It always depends on the scope of the objects you inject into your bean.
If one the following objects you inject: EmployeeFacade, CompanyFacade or DeviceFacade has a smaller scope, it will fail. In other words, you can only inject a bean with the same or a higher scope. I never tried this: request scoped property in session scoped JSF bean but it may help you out if you want to inject a smaller scoped object.

Resources