错误 java.lang.NullPointerException JSF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33139704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:14:16  来源:igfitidea点击:

Error java.lang.NullPointerException JSF

javajsfjsf-2primefaces

提问by jhuamanchumo

I'm using jsf/primefaces, I'm getting this npe, I'm invoking a method "registrar" from a form but appears that I'm doing it wrong. I have the class ProfesorBean.java and the view edicion.xhtml

我正在使用 jsf/primefaces,我得到了这个 npe,我从一个表单中调用了一个方法“注册器”,但似乎我做错了。我有类 ProfesorBean.java 和视图 edicion.xhtml

**Class: ProfesorBean**
...............................................

@ManagedBean(name="profesorBean")
@SessionScoped

public class ProfesorBean {
    private Profesor profesor;
    private List<Profesor> profesores;
    private Integer autogenerado=2;

public String registrar(){
    if (profesor.getCodigo()!=null) {             
    }else {
        profesor.setCodigo(autogenerado);
        profesores.add(profesor);
        autogenerado++;
    }
    profesor=null;
    return "listado";
}

}
...............................................

Here is the part of form that is submitted edicion.xhtml

这是提交edicion.xhtml的表单部分

...............................................
<f:facet name="footer">
                        <p:commandButton value="#{msjs.form_boton_registrar}" 
                                         action="#{profesorBean.registrar}"
                                         update="@form"
                                         ajax="false"
                                        />
                        <p:commandButton value="#{msjs.form_boton_limpiar}" 
                                         update="panel" 
                                         process="@this"  >
                            <p:resetInput target="panel" />
                        </p:commandButton>
                        <p:commandButton  action="listado"
                                         value="#{msjs.form_boton_regresar}"
                                         process="@this"
                                         immediate="true"
                                         ajax="false"/>
                    </f:facet>

Here is the error

这是错误

FATAL:   JSF1073: se ha interceptado javax.faces.FacesException durante el procesamiento de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Mensaje=#{profesorBean.registrar}: java.lang.NullPointerException
FATAL:   #{profesorBean.registrar}: java.lang.NullPointerException
javax.faces.FacesException: #{profesorBean.registrar}: java.lang.NullPointerException
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
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.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.access0(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:745)

Caused by: javax.faces.FacesException: #{profesorBean.registrar}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
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)
... 30 more

Caused by: 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)
... 34 more

Caused by: java.lang.NullPointerException
at pe.edu.cibertec.managed.ProfesorBean.registrar(ProfesorBean.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.el.parser.AstValue.invoke(AstValue.java:275)
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

采纳答案by Tom Sebastian

The problem is you are not initializing the member variable private Profesor profesor. I don't know what you are trying to do inside registrar. But anyway before using an object it should be initilized. You are getting NullpointerException since while calling profesor.getCodigo()the profesoris null. Please check it.

问题是您没有初始化成员变量private Profesor profesor。我不知道您要在注册商内部做什么。但无论如何,在使用对象之前,它应该被初始化。你得到NullPointerException异常,因为在调用profesor.getCodigo()profesornull。请检查一下。