2013-04-22 51 views
-2

我是新的Java EE中,我試圖創建一個從數據庫中收集信息的方法。它給我以下錯誤:java.lang.NullPointerException查詢調用

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; 
} 

感謝所有幫助你可以給。我一直堅持這個錯誤三天了。

+0

它與JSF有關嗎?如果是這樣的標籤。 – Lion 2013-04-22 22:27:04

+0

checkmatiere課程的49行內容是什麼? – 2013-04-22 22:33:33

+0

在第49行檢查你的n值 – 2013-04-22 22:39:57

回答

0

如果說,當你在這行叫trouver()你的錯誤是:

result1 = chapitredao.trouver(nummatiere); 

那麼問題是,你還沒有初始化chapitredao領域。也就是說,可能的地方你身邊類,你必須:

private <TYPE> chapitredao; 

使它像:

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

比如,例如:

private ChapitreDao chapitredao = new ChapitreDao(); 

或通過得到它,也許ChapitreDao chapitredao = ChapitreDao.getInstance();或者通過構造函數初始化它(甚至通過注入)。

+0

我初始化了chapitredao = new ChapitreDao();但問題仍然存在,我認爲每次他通過方法問題調用搜索時,他都會返回一個javax.persistence.NoResultException – user2281794 2013-04-22 22:49:08

+0

我不認爲這是一個初始化問題,因爲他在我使用matieredao時沒有調用它(並且它工作正常)@acdcjunior – user2281794 2013-04-22 22:53:34

+1

你可以看到,通過堆棧跟蹤,它永遠不會進入求解方法。您應該在該方法的第一行放置一個System.out.print(),以確保它永遠不會被輸入。 – acdcjunior 2013-04-23 01:55:56