这是我的类和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;
}
实际上有一个应用程序,在提交该表单时,将电子邮件发送给申请人,以便我尝试获取用户刚刚提交的电子邮件ID等详细信息,这些详细信息暂时保存在bean类中:< / p>
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;
}
}
我的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>
请解决这个错误,自从过去两天以来我一直在尝试无法解决它... 我已经尝试了一切,但无法解决。 错误:
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是:
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;
}
答案 0 :(得分:0)
问题是因为你没有在xml中声明mailSender bean,所以它是null。将下面的代码添加到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>
您还需要在@Autowired
类中声明EmpLeaveApplyController
mailSender字段
@Autowired
private JavaMailSender mailSender;
然后在调用sendMail方法之前将mailSender设置为MailMail实例,如下例所示:
MailMail leaveEmail=new MailMail();
//set the mailSender to the MailMail class
leaveEmail.setMailSender(mailSender);
boolean statusEmail=leaveEmail.sendMail(leaveapplyform);