需要解决方案请问如何解决这个错误?

时间:2017-03-18 12:20:15

标签: spring jsf nullpointerexception autowired

我使用primefaces spring和JSF,当我从my bean调用一个方法时 它从视图中检索属性,但是当它到达我的服务方法以通过调用dao方法在数据库中插入时会出现此错误

==============> begin 
Nom ==>Charafi
prenom ==>Saad
telephone ==>0622036006
MAT ==>A885511
DAte ==>Wed Mar 01 00:00:00 GMT+01:00 2017
mail ==>mrsaad.charafi@gmail.com
==============> end 
mars 18, 2017 12:14:45 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute      
Avertissement: java.lang.NullPointerException
javax.el.ELException: java.lang.NullPointerException
at org.apache.el.parser.AstValue.invoke(AstValue.java:292)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at     javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.primefaces.barcelona.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:32)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at ma.controle.gestion.bean.RessourceBean.save(RessourceBean.java:62)
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 org.apache.el.parser.AstValue.invoke(AstValue.java:279)
... 34 more

mars 18, 2017 12:14:45 PM com.sun.faces.context.AjaxExceptionHandlerImpl  handlePartialResponseError
Grave: java.lang.NullPointerException
at ma.controle.gestion.bean.RessourceBean.save(RessourceBean.java:62)
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 org.apache.el.parser.AstValue.invoke(AstValue.java:279)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.primefaces.barcelona.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:32)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

这是我的观点

          <h:form>       
      <div class="ui-g">
        <div class="ui-g-12">
            <div class="card">
                <h3>Create</h3>


                         <p:panelGrid columns="4" columnClasses="ui-grid-col-2,ui-grid-col-4,ui-grid-col-2,ui-grid-col-4" layout="grid" styleClass="ui-panelgrid-blank form-group" style="border:0px none; background-color:transparent;">
                            <p:outputLabel for="nom" value="Nom"/>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="nom" value="#{ressourceBean.ressource.nom}" />
                                <label>nom</label>
                            </h:panelGroup>

                            <p:outputLabel for="mail" value="Mail"/>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="mail" value="#{ressourceBean.ressource.mail}"/>
                                <label>Mail</label>
                            </h:panelGroup>



                            <p:outputLabel for="prenom" value="Prenom"/>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="prenom" value="#{ressourceBean.ressource.prenom}"/>
                                <label>prenon</label>
                            </h:panelGroup>

                            <p:outputLabel for="telephone" value="Telephone"/>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="telephone" value="#{ressourceBean.ressource.telephone}"/>
                                <label>Telephone</label>
                            </h:panelGroup>


                            <p:outputLabel for="matricule" value="Matricule"/>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="matricule" value="#{ressourceBean.ressource.matricule}"/>
                                <label>Matricule</label>
                            </h:panelGroup>



                            <p:outputLabel for="dateEntree" value="Date d'entree"/>
                            <p:calendar id="dateEntree" value="#{ressourceBean.ressource.dateEntree}"/>




                            </p:panelGrid>

                                <p:commandButton icon="ui-icon-save" actionListener="#{ressourceBean.save}" value="Save" style="display:inline-block;margin-top:5px"/>


               </div>
        </div>
    </div>
  </h:form>

这是我的bean RessourceBean

package ma.controle.gestion.bean;


import java.io.Serializable;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;

import ma.controle.gestion.modele.Ressource;
import ma.controle.gestion.service.IRessourceService;

@ManagedBean(name="ressourceBean")
@ViewScoped
public class RessourceBean implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

private String nom;
private String prenom;
private String matricule;
private String mail;
private String telephone;
private Date dateEntree;

private final Ressource ressource = new Ressource();


@Autowired
private IRessourceService ressourceService;

public RessourceBean(String nom, String prenom, String matricule, String mail, Date dateEntree, String telephone) {
    super();
    this.nom = nom;
    this.prenom = prenom;
    this.matricule = matricule;
    this.mail = mail;
    this.dateEntree = dateEntree;
    this.telephone=telephone;
}

public RessourceBean() {
    super();
}

public void save(){

    System.out.println("==============> begin ");
    System.out.println(" Nom ==>" + getRessource().getNom() );
    System.out.println(" prenom ==>" + getRessource().getPrenom() );
    System.out.println(" telephone ==>" + getRessource().getTelephone());
    System.out.println(" MAT ==>" + getRessource().getMatricule() );
    System.out.println(" DAte ==>" + getRessource().getDateEntree() );
    System.out.println(" mail ==>" + getRessource().getMail() );
    System.out.println("==============> end ");

    ressourceService.addRessource(ressource);
    System.out.println("==============> insertion finished ");
}



public String getTelephone() {
    return telephone;
}

public void setTelephone(String telephone) {
    this.telephone = telephone;
}

public IRessourceService getRessourceService() {
    return ressourceService;
}

public void setRessourceService(IRessourceService ressourceService) {
    this.ressourceService = ressourceService;
}

public Ressource getRessource() {
    return ressource;
}

public String getNom() {
    return nom;
}

public void setNom(String nom) {
    this.nom = nom;
}

public String getPrenom() {
    return prenom;
}

public void setPrenom(String prenom) {
    this.prenom = prenom;
}

public String getMatricule() {
    return matricule;
}

public void setMatricule(String matricule) {
    this.matricule = matricule;
}

public String getMail() {
    return mail;
}

public void setMail(String mail) {
    this.mail = mail;
}

public Date getDateEntree() {
    return dateEntree;
}

public void setDateEntree(Date dateEntree) {
    this.dateEntree = dateEntree;
}   

}

这是我的服务RessourceServiceImpl

的实现
package ma.controle.gestion.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ma.controle.gestion.dao.IRessourceDao;
import ma.controle.gestion.modele.Ressource;


@Transactional
@Service("ressourceService")
public class RessourceServiceImpl implements IRessourceDao {

@Autowired
private IRessourceDao ressourceDao;


@Override
public void addRessource(Ressource ressource) {
    System.out.println("Service Baby");
    ressourceDao.addRessource(ressource);
}


public IRessourceDao getRessourceDao() {
    return ressourceDao;
}


public void setRessourceDao(IRessourceDao ressourceDao) {
    this.ressourceDao = ressourceDao;
}


}

这是我的dao RessourceDaoImpl

的实现
package ma.controle.gestion.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ma.controle.gestion.dao.IRessourceDao;
import ma.controle.gestion.modele.Ressource;

@Repository("ressourceDao")
public class RessourceDaoImpl implements IRessourceDao {


@Autowired
private SessionFactory sessionFactory; 

@Override
public void addRessource(Ressource ressource) {
    sessionFactory.getCurrentSession().save(ressource);
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}
}

1 个答案:

答案 0 :(得分:1)

我猜您的@Autowired resourceService未设置,因为您希望它是IRessourceService,但您提供的是:

@Service("ressourceService")
public class RessourceServiceImpl implements IRessourceDao

因此bean容器无法将其拾取,将您的Autowired注释更改为:

@Autowired
private IRessourceDao ressourceService;