自动连线的JavaMailSender上的空指针

时间:2018-09-27 19:54:59

标签: spring nullpointerexception javamail

为什么这会为变量抛出Null-pointer异常 私有静态JavaMailSender在“ sender.createMimeMessage()”中使用时会显示出来吗?

我正在尝试测试此方法“ SendTicketMail”,这就是为什么我将其声明为静态(因为我的主体需要它),并且同样需要将发件人声明为静态。

如果这是问题所在,我如何在不将发件人或SendTicket声明为静态的情况下进行测试?

package sof.eng.project.Tickets;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import soft.eng.project.Event.Event;
import soft.eng.project.Parent.Parent;
import soft.eng.project.Provider.Provider;

public class TicketsController {

    @Autowired
    private static JavaMailSender sender;

    static void SendTicketMail(Parent parent, Provider provider, Event event, int Tickets, int TotalPrice) throws MessagingException, DocumentException, IOException
            {
                MimeMessage mimeMessageObj = sender.createMimeMessage();
                MimeMessageHelper messageObj = new MimeMessageHelper(mimeMessageObj, true, "UTF-8");
               // messageObj.setFrom(fromEmail);
                messageObj.setTo(parent.getEmail());
                messageObj.setSubject("Your ticket for...");
                messageObj.setText("<strong>See The Attached</strong>", true);

                String firstName = parent.getFirstName();
                String lastName = parent.getLastName();
                String eventname = event.getTitle();
                String fileName = "tickets.pdf";

                PDFConfiguration.makePDF(fileName, firstName, lastName, eventname, Tickets, TotalPrice);


                byte[] data = null;
                try {
                    data = readFully(fileName);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                messageObj.addAttachment(""+ eventname + "-Ticket.pdf", new ByteArrayResource(data));
            }

    public static byte[] readFully(String sourcePath) throws IOException
            {
                InputStream stream = new FileInputStream(sourcePath);

                byte[] buffer = new byte[8192];
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                int bytesRead;
                while ((bytesRead = stream.read(buffer)) != -1)
                {
                    baos.write(buffer, 0, bytesRead);
                }
                return baos.toByteArray();
            }

    public static void main(String[] args) throws MessagingException, DocumentException, IOException 
    {
        Parent parent = new Parent();
        parent.setFirstName("Max");
        parent.setLastName("Maximus");
        parent.setEmail("kanic123@gmail.com");
        parent.setPassword("asda");
        parent.setAddress("address");

        Provider provider = new Provider();
        provider.setFirstName("firstName");
        provider.setLastName("lastName");
        provider.setPassword("asda");
        provider.setCompanyName("asdaCorps");
        provider.setEmail("asda@gmail.com");
        provider.setBankAccount("123asda123");
        provider.setAddress("address Str");
        provider.setStatus("normal");

        Event event = new Event();
        event.setEmail("email");
        event.setTickets(1000);
        event.setPrice(10);
        event.setAddress("address");
        event.setMinAge(5);
        event.setMaxAge(15);
        event.setCategory("athletics");
        event.setTicketsLeft(100);
        event.setReleaseDate(null);
        event.setKickoffDate(null);
        event.setDescription("description");

        int Tickets = 10;
        int TotalPrice = 100;
        SendTicketMail(parent,provider,event,Tickets, TotalPrice);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要启动Spring应用程序上下文。 如果要运行测试,我建议您使用JUnit和SpringJUnit4ClassRunner