在搜索了几个小时之后,我终于找到了导致nullpointerexception的原因。它是我在QuestionController.create()中使用属性Survey的。当我注释掉getSurvey()行时,页面工作得很好。所有其他功能也可以正常工作。只有QuestionController.create()不起作用。我知道我注入的managedProperty可以在页面上正确显示surveyname(questionController.survey.surveyName)。
有没有人知道为什么我的QuestionController在尝试使用create()函数时会变为Null?
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;
}
}
我的托管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;
}
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>
感谢您一看!
答案 0 :(得分:1)
您在Survey
@PostConstruct
@PostConstruct
private void init(){
setSurvey(getSessionController().getSurvey());
//But Survey survey; is null of SessionController
//getSessionController().getSurvey() is giving you null
}
但是 Survey survey;
SessionController
null
你需要初始化它,所以你必须首先实例化{{1}在构造函数中或确保它在使用之前不为null。
Survey
评论后编辑:
好吧,我认为public class SessionController extends Controller{
private Survey survey;//Not Initialized
private String emailAddress;
private String password;
...
正在返回getSurvey().getQuestions()
,这是导致null
的原因。
答案 1 :(得分:1)
也许调查的问题列表尚未初始化。所以在你有问题之前,你应该检查一下survey.getQuestions()是否为null,如果你是初始化它。