简体   繁体   English

Spring Mail中的空指针异常

[英]Null pointer exception in spring mail

Here is my class and the xml file: 这是我的课程和xml文件:

public class MailMail {


    private MailSender mailSender;

    private SimpleMailMessage MailMessage;
    public void setMailSender(MailSender mailSender) {  
        this.mailSender = mailSender;  
    }  



    public boolean sendMail(LeaveApplyForm leaveApplyForm) throws MailException
    {
        SimpleMailMessage message = new SimpleMailMessage();

        EmpRegistrationForm empRegistrationForm=new EmpRegistrationForm();
        String to=leaveApplyForm.getFirstApprover();

    //  String to1=leaveApplyForm.getFinalApprover();
        String text=leaveApplyForm.getReason();

        String from=empRegistrationForm.getEmail();
        String subject="Application for the Leave";

        message.setFrom(from);

                message.setTo(to);

                message.setSubject(subject);

                message.setText(text);

                mailSender.send(message);


    return true;    

    }

actually there is a application,on submitting that form the email shold be sent to the applicant so I am trying to get details like email id which has just now submitted by the user and These details are temporerly saved in bean class: 实际上有一个应用程序,在提交表单时将电子邮件地址发送给申请人,所以我试图获取诸如用户刚刚提交的电子邮件ID之类的详细信息,这些详细信息已临时保存在bean类中:

private String LeaveType;
    private String leavePeriod;

    private String reason;
    private String FirstApprover;

    private String finalApprover;
    private String selectHours;

    public String getLeaveType() {
        return LeaveType;
    }

    public void setLeaveType(String leaveType) {
        LeaveType = leaveType;
    }

    public String getLeavePeriod() {
        return leavePeriod;
    }

    public void setLeavePeriod(String leavePeriod) {
        this.leavePeriod = leavePeriod;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getFirstApprover() {
        return FirstApprover;
    }

    public void setFirstApprover(String firstApprover) {
        FirstApprover = firstApprover;
    }

    public String getFinalApprover() {
        return finalApprover;
    }

    public void setFinalApprover(String finalApprover) {
        this.finalApprover = finalApprover;
    }

    public String getSelectHours() {
        return selectHours;
    }

    public void setSelectHours(String selectHours) {
        this.selectHours = selectHours;
    }
}

and my s-servlet.xml is: 而我的s-servlet.xml是:

<context:component-scan base-package="com.controller" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
   <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>     
        <property name="defaultEncoding" value="UTF-8"/>
  </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>      
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
</bean>      

please solve this error I have been trying since last two days not able to solve it... I have tried everything but could not worked out. 请解决此错误,因为最近两天以来我一直无法解决此错误...我已经尝试了所有方法,但无法解决。 error: 错误:

11:01:37,304 DEBUG AnnotationMethodHandlerExceptionResolver:132 - Resolving exception from handler [com.controller.EmpLeaveApplyController@1647278]: java.lang.NullPointerException
11:01:37,307 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [com.controller.EmpLeaveApplyController@1647278]: java.lang.NullPointerException
11:01:37,307 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [com.controller.EmpLeaveApplyController@1647278]: java.lang.NullPointerException
11:01:37,309 DEBUG DispatcherServlet:878 - Could not complete request
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:863)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:767)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause

java.lang.NullPointerException
    com.controller.MailMail.sendMail(MailMail.java:43)
    com.controller.EmpLeaveApplyController.processForm(EmpLeaveApplyController.java:78)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:767)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

EmpleavapplyController is: EmpleavapplyController是:

static Logger log = Logger.getLogger(EmpLeaveApplyController.class.getName());

    @RequestMapping(value = "/leaveapplyform", method = RequestMethod.GET)
       public String showForm(ModelMap model,HttpServletRequest request) 
       {
         log.info("Inside Controller returning to leaveapplyform page....");

         LeaveApplyForm leaveapplyform =  new LeaveApplyForm();
        model.put("leaveapplyform", leaveapplyform);    

        /*EmpLeaveApplyWorker worker1=new EmpLeaveApplyWorker();*/



    return GlobalConstants.LEAVE_APPLY; 
       }

    @RequestMapping(value = "/leaveapplyform" ,method = RequestMethod.POST)
    public String processForm(@ModelAttribute("leaveapplyform") LeaveApplyForm leaveapplyform,HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {
        leaveapplyform = (LeaveApplyForm) model.get("leaveapplyform");

        /*if(result.hasErrors()){
            return GlobalConstants.ERRORPAGE;
        }*/



        EmpLeaveApplyWorker worker=new EmpLeaveApplyWorker();
        boolean status=worker.validateUser(leaveapplyform);
        if(status)
        {
            MailMail leaveEmail=new MailMail();
            boolean statusEmail=leaveEmail.sendMail(leaveapplyform);
            if(statusEmail)
            {

                System.out.println("Now you are sending the mail");
            }


        else{

                System.out.println("you are not reaching to the mail");
            }

            System.out.println(status);
            System.out.println("------------------came to the last poi8nt of todays task-------------------------------");
            return GlobalConstants.HOME_PAGE;
        }
        else
        {System.out.println(status);
            return GlobalConstants.LEAVE_APPLY;
        }

The issue is because you are not declaring the mailSender bean in the xml, so it is null. 问题是因为您没有在xml中声明mailSender bean,所以它为null。 Add the code below to the s-servlet.xml 将下面的代码添加到s-servlet.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="xxxx@gmail.com" />
<property name="password" value="xxxx" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>

You also need to declare @Autowired mailSender field in the EmpLeaveApplyController class 您还需要在EmpLeaveApplyController类中声明@Autowired mailSender字段

@Autowired
private JavaMailSender mailSender;

and then set the mailSender to the MailMail instance before invoking the sendMail method, like the example below: 然后在调用sendMail方法之前将mailSender设置为MailMail实例,如下例所示:

 MailMail leaveEmail=new MailMail();
 //set the mailSender to the MailMail class
 leaveEmail.setMailSender(mailSender);
 boolean statusEmail=leaveEmail.sendMail(leaveapplyform);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM