Roy
Roy

Reputation: 102

NullPointerException in Spring when sending mail with SimpleMailMessage()

I've been learning how to make a Java Spring Web Service, but I've run into an issue when trying to send mail.

When I try to send it through my controller the code works just fine, but I'm trying to get the code in a separate class and I suddenly get a NullPointerException, even though the code is the same (as far as I can tell).

This is the function that is currently giving the issue:

package com.tenshin_arts.inventory.mail;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.tenshin_arts.inventory.dao.InventoryItem;
import com.tenshin_arts.inventory.dao.User;
import com.tenshin_arts.inventory.service.InventoryService;

public class InventoryMail {


       @Autowired
       private MailSender mailSender;

    public void stockBelowMinimumUpdate(InventoryItem inventoryItem) {

        String email = "[email protected]";
        String text= "";

        text += "Het volgende product is onder het minimum gekomen:\n\n";
        text += "Productnaam: " + inventoryItem.getName();
        text += "\nProductcode: " + inventoryItem.getBarcode();
        text += "\nHuidige Voorraad: " + inventoryItem.getCurrentStock();
        text += "\nMinimum Voorraad: " + inventoryItem.getStockMinimum();
        text += "\nPrijs: " + inventoryItem.getPrice();
        text += "\nLeverancier: " + inventoryItem.getSupplier();
        text += "\n\n\nBericht voor de leverancier:";
        text += "\n\n" + inventoryItem.getBarcode();
        text += "\t" + inventoryItem.getName();
        text += "\t[aantal hier] x €" + inventoryItem.getPrice();

            System.out.println("email running");

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom("[email protected]");
        mail.setTo(email);
        mail.setSubject("Test is gelukt!");
        mail.setText(text);



        try {
            mailSender.send(mail); //This line produces the exception
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Can't send message");
        }


    }
}

This is the stack trace:

java.lang.NullPointerException
    at com.tenshin_arts.inventory.mail.InventoryMail.stockBelowMinimumUpdate(InventoryMail.java:53)
    at com.tenshin_arts.inventory.dao.InventoryDao.checkStockForMail(InventoryDao.java:152)
    at com.tenshin_arts.inventory.service.InventoryService.checkStockForMail(InventoryService.java:52)
    at com.tenshin_arts.inventory.controllers.InventoryController.databaseCallMutateItem(InventoryController.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    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.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    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:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    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:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source) 

And here is my bean configuration:

<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="[email protected]" />
        <property name="password" value="xxxxx" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
            </props>
        </property>
    </bean> 

The weird thing is, in my mapping controller, I have the following code that runs just fine (removed the functions that aren't related to the mailing):

package com.tenshin_arts.inventory.controllers;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tenshin_arts.inventory.dao.InventoryItem;
import com.tenshin_arts.inventory.dao.User;
import com.tenshin_arts.inventory.service.InventoryService;
import com.tenshin_arts.inventory.service.UsersService;

@Controller
public class InventoryController {

    private InventoryService inventoryService;
    private InventoryItem currentItem;
    private UsersService usersService;

    @Autowired
    private MailSender mailSender;

@RequestMapping(value="/sendmessage")
    @ResponseBody
    public void sendMessage(Principal principal) {
        UserDetails userDetails =
                 (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        List<InventoryItem> inventoryItemsList = inventoryService.getCurrent();



        User currentUser = usersService.getUserEmail(userDetails.getUsername());
        String email = currentUser.getEmail();
        String text= "";

        for(InventoryItem s : inventoryItemsList) {

            text += s.getBarcode();
            text += "\t";
            text += s.getName();
            text += "\t";
            text += s.getCurrentStock();
            text += "\t";
            text += s.getPrice();
            text += "\n";
        }




        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom("[email protected]");
        mail.setTo(email);
        mail.setSubject("Test is gelukt!");
        mail.setText(text);

        try {
            mailSender.send(mail); 
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Can't send message");
        }


    }



}

I already checked my SimpleMailMessage 'mail' through a system.out, so I think the issue lies somewhere with my MailSender bean/wiring, but I don't know what since I wire it in the same way.

Can you please help me solve my problem?

Upvotes: 2

Views: 4548

Answers (1)

Sync
Sync

Reputation: 3777

Why does it throw a NullPointerException?

This indicates mailSender is null. It is null because Spring could not autowire a bean instance to mailSender. Spring is not able to do that because it could not tell InventoryMail itself is a bean. Because Spring could not tell InventoryMail is a bean, it does not detect the @Autowired annotation.

Why does it work in InventoryController?

That is because the annotation @Controller tells Spring that InventoryController is a bean, thus enabling Spring to detect @Autowired.

Proposed solution

Annotate your InventoryMail class with @Component to tell Spring InventoryMail is a bean. This enables Spring to detect @Autowired.

Upvotes: 8

Related Questions