0

enter image description hereenter image description hereMy function to send email

    @Autowired
    private MailService mailService;[![enter image description here][2]][2]
    public void sendMailConfirm(String receiverAddress, int orderId) {
        Mail mail = new Mail();
        mail.setMailFrom(senderAddress);
        mail.setMailTo(receiverAddress);
        mail.setMailSubject(mailSubject);
        mail.setMailContent(mailContent + "\n\nOrder ID: " + orderId);

        mailService.sendMail(mail);
    }

My Mail class


public class Mail {
    private String mailFrom;

    private String mailTo;

    private String mailSubject;

    private String mailContent;

    private String contentType;

    private List<Object> attachments;

    getter & setter

I wrote this in an API. But when I test this. It returns a null exception. Please help me!

                //  send mail to notif success order
                Manager manager = new Manager();
                manager.sendMailConfirm(orderVM.getEmail(), order.getId());
6
  • 2
    Error stack please . Also the use of new suggests you are not using spring the correct way Commented Feb 5, 2020 at 8:31
  • I'm just uploading an image. How to fix that issue? Commented Feb 5, 2020 at 10:29
  • The information shared is not adequate to help you. Here the order or mailService could be null, but that is a guess. Could you please share all the code that will reproduce this issue . Please try to improve the question with enough information to help you out Commented Feb 5, 2020 at 10:34
  • @R.G I don't understand. the orderVM.getEmail() and order.getId() always return result not null. Why the function can get null? Commented Feb 5, 2020 at 10:52
  • the attachments is null when I'm sending the order. Commented Feb 5, 2020 at 10:53

1 Answer 1

1

The NullPointerException apparently occur here:

 mailService.sendMail(mail);

Make sure that the mailService in your Manager class is not null.

If you are in a Spring Boot context the Manager probably should not be initialized manually but that's just an assumption, as I don't know the rest of the code.

Sign up to request clarification or add additional context in comments.

6 Comments

But I autowired it.
I mean, you create Manager manager = new Manager(); by yourself. Or did I miss something?
As soon as you create the the Manager by yourself, nothing within it gets autowired. But you could autowire the Manager itself. Then the MailService will be probably injected.
How can my mailService can be null? I don't understand
I resolved. I wrote that function into MailService class then I can use it correctly. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.