尝试发送电子邮件空指针异常

时间:2013-04-04 09:20:09

标签: java email nullpointerexception

我正在尝试从数据库获取电子邮件地址,使其成为“InternetAddress收件人”,但它返回nullPointerException并且没有关于该问题的更多信息。 这是代码:

public class SendEmail implements ISendMail {

@Override
public void sendEmail(Action action) {
    if (action.getCopieAction() != null) {
        try {

            Properties props = new Properties();
            Session session = Session.getInstance(props);
            Message message = new MimeMessage(session);
            UtilisateurDao user = new UtilisateurDao();
            // récupération du code user de l'utilisateur

            Integer codeUserEnCopie = action.getCopieAction();
            System.out.println(codeUserEnCopie);
            Utilisateur util = user.findUserByCode(codeUserEnCopie);
            String mailUserCopie = util.getEMAIL();

            InternetAddress recipient = new InternetAddress(mailUserCopie);
            message.setRecipient(Message.RecipientType.TO, recipient);
            message.setSubject("Vous êtes en copie d'une action");
            message.setText("Bonjour, l'utilisateur "
                    + action.getUtilCreation()
                    + " vous à mis en copie d'une action:"
                    + "Nom de l'action : " + action.getObjetAction()
                    + "Détails de l'action : " + action.getTexteAction()
                    + "Date de l'action : " + action.getDateAction());

            Transport.send(message);
        } catch (NoSuchProviderException e) {
            System.err.println("Pas de transport disponible pour ce  
                            protocole");
            System.err.println(e);
        } catch (AddressException e) {
            System.err.println("Adresse invalide");
            System.err.println(e);

        } catch (MessagingException e) {
            System.err.println("Erreur dans le message");
            System.err.println(e);

        }

    }

}

}

将用户置于数据库中的功能

public Utilisateur findUserByCode(Integer codeUser) {
System.out.println(codeUser);
    Utilisateur util = new Utilisateur();
    util = null;

    try{

        System.out.println("ddd");
        Query requete = em.createQuery("SELECT u FROM Utilisateur u WHERE 
                    u.codeUtilisateur="+codeUser);
        requete.setParameter("codeuser", codeUser);
        util = (Utilisateur) requete.getSingleResult();

    }catch(NoResultException e){

        System.out.println(e.getMessage() + e.getCause());
        FacesMessage message = new FacesMessage(e.getMessage() + e.getCause());
        FacesContext.getCurrentInstance().addMessage(null, message);
        e.printStackTrace(); 
    }


    return util;
}

  and the fonction that calls the fonction sendEmail

   public void creerAction(Action action) {


    try {
        ISendMail sendMail = new SendEmail();
        sendMail.sendEmail(action);
        System.out.println(action.getCopieAction());
        em.persist(action);

        em.flush();

        FacesMessage message = new FacesMessage("Action  " + 
  action.getObjetAction() + " ajouté avec succès avec en copie"+ action.getCopieAction());
        FacesContext.getCurrentInstance().addMessage(null, message);
    } catch (PersistenceException e) {
        System.out.println(e.getMessage());
    }
}

如何在错误中获得更多信息?

堆栈跟踪:

javax.faces.el.EvaluationException: javax.ejb.EJBException
at javax.faces.component.MethodBindingMethodExpressionAdapter.
   invoke(MethodBindingMethodExpressionAdapter.java:102)
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:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
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:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
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:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
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:662)
    Caused by: javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.
    invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.
    invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at $Proxy699.creerAction(Unknown Source)
at beans.ActionBean.ajouter(ActionBean.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.
     invoke(MethodBindingMethodExpressionAdapter.java:88)
... 32 more
    Caused by: java.lang.NullPointerException
at dao.UtilisateurDao.findUserByCode(UtilisateurDao.java:157)
at dao.SendEmail.sendEmail(SendEmail.java:33)
at dao.ActionDao.creerAction(ActionDao.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.glassfish.ejb.security.application.EJBSecurityManager.
    runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.
    invoke(EJBSecurityManager.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.
    invokeNext(InterceptorManager.java:800)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.
    doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.
     aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.GeneratedMethodAccessor615.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.
    intercept(InterceptorManager.java:861)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.
    invokeNext(InterceptorManager.java:800)
at com.sun.ejb.containers.interceptors.InterceptorManager.
     intercept(InterceptorManager.java:370)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.
    invoke(EJBLocalObjectInvocationHandler.java:214)
... 43 more

   the DAO

public Utilisateur findUserByCode(Integer codeUser) {
System.out.println(codeUser);
    Utilisateur util = new Utilisateur();
    util = null;

    try{

        System.out.println("ddd");
        Query requete = em.createQuery(SELECT_BY_CODE_USER);
        requete.setParameter("codeuser", codeUser);
        util = (Utilisateur) requete.getSingleResult();

    }catch(NoResultException e){

        System.out.println(e.getMessage() + e.getCause());
        FacesMessage message = new FacesMessage(e.getMessage() + e.getCause());
        FacesContext.getCurrentInstance().addMessage(null, message);
        e.printStackTrace(); 
    }


    return util;
}

1 个答案:

答案 0 :(得分:1)

看看这一行:

at dao.UtilisateurDao.findUserByCode(UtilisateurDao.java:157)

你的例外应该来自那里。