Skip to content
Advertisement

NullPointerException while send a otp mail using springboot

I was trying to send a otp mail using springboot. I have already created a login page from where the mail will be provided to sendotp(). Or you can help by providing code for how to send mail otp using springboot. Here is my code :

OtpControllerclass –

JavaScript

EmailSenderService class :

JavaScript

Wnen I am calling sendOtp() method I get NullPointerException errror in springboot.

error :

JavaScript

I know there is some problem in OtpController but can’t figure out what exactly I have to.

Advertisement

Answer

You controller need to know that emailService should be injected by Spring.

You can add @Autowired annotation:

JavaScript

Or create a all args constructor

JavaScript

Because class is marked as @Controller, Spring will inject beans though the constructor tying to resolve all parameters.

With second approach you can make emailService final.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement