Reputation: 95
After searching for hours I finally found what caused a nullpointerexception. It's the use of the property Survey in my QuestionController.create(). When I comment out the line getSurvey().. the page works just fine. All other functionality works fine as well. Only the QuestionController.create() doesn't work. I know my injected managedProperty works as the surveyname (questionController.survey.surveyName) displays correctly on the page.
Does anyone know why my QuestionController turns Null when trying to use the create() function?
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
at com.survey.jsfController.QuestionController.create(QuestionController.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 35 more
My Managed bean QuestionController
@SessionScoped
@ManagedBean(name = "questionController")
public class QuestionController extends Controller implements Serializable {
private QuestionService questionService;
private QuestionTypeService questionTypeService;
public QuestionController() {
setQuestionService(ApplicationController.getQuestionService());
setQuestionTypeService(ApplicationController.getQuestionTypeService());
question = new Question();
}
@PostConstruct
private void init(){
setSurvey(getSessionController().getSurvey());
}
private static final long serialVersionUID = 1L;
private Question question;
private Survey survey;
private String surveyId;
@ManagedProperty("#{sessionController}")
private SessionController sessionController;
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
public List<Question> getQuestions() {
return getQuestionService().retrieveAll();
}
public List<QuestionType> getQuestionTypes() {
return getQuestionTypeService().retrieveAll();
}
public void create() {
//TODO: link survey with this question
getSurvey().getQuestions().add(getQuestion());
//surveyService.update(survey);
questionService.create(getQuestion());
FacesContext.getCurrentInstance().addMessage("questionCreation", new FacesMessage("Question has been created succesfully!"));
}
public String update() {
questionService.update(question);
FacesContext.getCurrentInstance().addMessage("questionCreation", new FacesMessage("Question has been updated succesfully!"));
return "overview";
}
public String delete() throws RemoteException {
getQuestionService().delete(question);
FacesContext.getCurrentInstance().addMessage("questionDeletion",
new FacesMessage("Question has been deleted succesfully!"));
return "overview";
}
public String show(Question question){
if (question != null) setQuestion(question);
return "detail";
}
public String update(Question question){
if (question != null) setQuestion(question);
return "update";
}
public String delete(Question question){
if (question != null) setQuestion(question);
return "delete";
}
public void setSurveyId(String survey) {
this.surveyId = survey;
}
public String getSurveyId() {
return this.surveyId;
}
@Override
public void clear(){
setQuestion(null);
FacesContext.getCurrentInstance().addMessage("formClearing", new FacesMessage("Form cleared!!", "The answer creation has been submitted successfully"));
}
public SessionController getSessionController() {
return sessionController;
}
public void setSessionController(SessionController sessionController) {
this.sessionController = sessionController;
}
public Survey getSurvey() {
return survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
public QuestionService getQuestionService() {
return questionService;
}
public void setQuestionService(QuestionService questionService) {
this.questionService = questionService;
}
public QuestionTypeService getQuestionTypeService() {
return questionTypeService;
}
public void setQuestionTypeService(QuestionTypeService questionTypeService) {
this.questionTypeService = questionTypeService;
}
}
My managed bean SessionController
@SessionScoped
@ManagedBean(name = "sessionController")
public class SessionController extends Controller{
private Survey survey;
private String emailAddress;
private String password;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
return "about";
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "login";
}
public Survey getSurvey() {
return survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
The questionCreate.xhtml
<p>Step 1: Created survey: #{questionController.surveyId}</p>
<p>
<strong>Step 2: Create questions</strong>
</p>
<p>Step 3: Create benchmark</p>
<p>Step 4: Create graph per question</p>
<p:panelGrid columns="2">
<f:facet name="header">Create question for survey #{questionController.survey.surveyName} </f:facet>
<h:outputText value="#{msg['question']}" for="question" />
<h:inputText id="question"
value="#{questionController.question.question}"></h:inputText>
<h:outputText value="#{msg['selectType']}" for="questionType" />
<p:selectOneMenu id="questionType"
value="#{questionController.question.questionType}" effect="fold"
editable="true" converter="questionTypeConverter">
<!-- selectItem used to display as reset value/empty value -->
<f:selectItem itemLabel="select questionType" itemValue=""/>
<!-- selectItems used to fill with value from a collection -->
<f:selectItems value="#{questionController.questionTypes}" var="q"
itemLabel="#{q.typeName}" />
</p:selectOneMenu>
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="#{msg['createButton']}"
action="#{questionController.create()}"/>
<h:commandButton value="Clear"
action="#{questionController.clear()}" immediate="true">
<f:resetValues render="form form:question" />
</h:commandButton>
</h:panelGroup>
</f:facet>
</p:panelGrid>
<h:messages style="color:red" />
<p />
<ui:include src="listRetrieveComponent.xhtml"></ui:include>
</ui:define>
Thanks for having a look!
Upvotes: 4
Views: 4897
Reputation: 304
Maybe the questions list of the survey haven't been initialized. so before you had a question you should check if survey.getQuestions() is null and if it is you initialize it.
Upvotes: 1
Reputation: 22972
You are setting Survey
in @PostConstruct
@PostConstruct
private void init(){
setSurvey(getSessionController().getSurvey());
//But Survey survey; is null of SessionController
//getSessionController().getSurvey() is giving you null
}
BUT Survey survey;
Of SessionController
is null
you have to initialized it so you have to first instantiate Survey
in constructor or make sure it's not null before using it.
public class SessionController extends Controller{
private Survey survey;//Not Initialized
private String emailAddress;
private String password;
...
EDIT After Comment:
Okay than I think getSurvey().getQuestions()
is returning null
that's what causes NPE
.
Upvotes: 1