java.lang.NullPointerException查询调用

时间:2013-04-22 22:24:09

标签: java java-ee

我是Java EE的新手,我正在尝试创建一个从数据库中收集信息的方法。它给了我以下错误:

java.lang.NullPointerException
at com.universite.bean.checkmatiere.check(checkmatiere.java:49)
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:601)
at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:528)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257)
at com.sun.el.parser.AstValue.getValue(AstValue.java:134)
at com.sun.el.parser.AstValue.getValue(AstValue.java:183)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
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:722)

这是我的check方法。当我调用trouver方法时,它会给我一个错误:

 public void check() {
    etudiant =(Etudiant) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("etudiant");
    n=etudiant.getniveau();
    result =matiereDao.trouver( n );
    if(result.size()!=0) {
        Iterator<Matiere> stIterator=result.iterator();
        while(stIterator.hasNext()){
         matiere=(Matiere)stIterator.next();
         FacesMessage message = new FacesMessage("matire: "+ matiere.getNom() );
         FacesContext.getCurrentInstance().addMessage( null,message );

        nummatiere=matiere.getIdmatiere();
            private List<Chapitre> result1;
    /*line 49*/  result1 =chapitredao.trouver( nummatiere );
          if (result1!= null)
         {if(result1.size()!=0)
                        {
                            Iterator<Chapitre> stIterator1=result1.iterator();
                            while(stIterator1.hasNext()){
                             chapitre=(Chapitre)stIterator1.next();
                             FacesMessage message1 = new FacesMessage( "chapitre: "+chapitre.getNomchapitre() );
                             FacesContext.getCurrentInstance().addMessage( null,message1 );

                            }
            }          }


         }

    }
    else  {
        FacesMessage message = new FacesMessage( "echec !" );
        FacesContext.getCurrentInstance().addMessage( null,message );
    }

这是我的“麻烦”功能

      private static final String JPQL_SELECT_PAR_nummatiere= "SELECT ufFROM              Chapitre uf where uf.nummatiere=:nummatiere";
               private static final String PARAM_nummatiere       = "nummatiere";
               public List<Chapitre> trouver(int nummatiere  ) throws DAOException {

Query requete1 = entitymanager.createQuery( JPQL_SELECT_PAR_nummatiere );
requete1.setParameter( PARAM_nummatiere, nummatiere);
try {
    results2 =  requete1.getResultList();
} catch ( NoResultException e ) {
return null;
} catch ( Exception e ) {
throw new DAOException( e );
}
return results2;
}

感谢您提供任何帮助。我已经坚持这个错误三天了。

1 个答案:

答案 0 :(得分:0)

如果您说错误是在此行中致电trouver()

result1 = chapitredao.trouver(nummatiere);

然后问题是您尚未初始化chapitredao字段。也就是说,你可能在课堂上的某个地方:

private <TYPE> chapitredao;

使它像:

private <TYPE> chapitredao = new <TYPE>();

例如:

private ChapitreDao chapitredao = new ChapitreDao();

或者通过,ChapitreDao chapitredao = ChapitreDao.getInstance();或通过构造函数(甚至通过注入)初始化它。