title: java.lang.NullPointerException what I'm doing wrong?
body: <p>I'm working on a project in which my intention is to run a Corn job and send mail to my friends to wishing them on their birthday, I was able to get email from MySQL DB and compared it to the current date but when it comes to sending an email I'm getting NullPointerException.</p>
  <p>I'm sure there is no problem with application properties I used them with other projects as well and they are function properly</p>
  <pre><code>//Imports
  package dev.teja.happybirthday;
  import com.fasterxml.jackson.annotation.JsonFormat;
  import lombok.*;
  import javax.mail.MessagingException;
  import javax.mail.internet.MimeMessage;
  import javax.persistence.*;
  import java.io.IOException;
  import java.nio.charset.StandardCharsets;
  import java.util.Date;
  import java.text.SimpleDateFormat;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import javax.validation.constraints.Email;
  import freemarker.template.*;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.mail.javamail.JavaMailSender;
  import org.springframework.mail.javamail.MimeMessageHelper;
  import org.springframework.scheduling.annotation.EnableScheduling;
  import org.springframework.scheduling.annotation.Scheduled;
  import org.springframework.stereotype.Service;
  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
  import org.springframework.web.bind.annotation.*;
  
  //Main
  @SpringBootApplication
  public class HappybirthdayApplication {
      public static void main(String[] args) {
          SpringApplication.run(HappybirthdayApplication.class, args);
      }
      @Autowired
      MyFriendsRepository myFriendsRepository ;
      @Scheduled(cron = &quot;*/30 * * * * *&quot;)
      void WishMyFriends() {
          List&lt;MyFriends&gt; myFriendsList = myFriendsRepository.findAll();
          myFriendsList.forEach(friend -&gt; {
              Date date = new Date();
              SimpleDateFormat formatter = new SimpleDateFormat(&quot;dd/MM&quot;);
              String CurrentDate= formatter.format(date);
              String GivenDate = formatter.format(friend.dob);
              if (GivenDate.equals(CurrentDate)){
                  Job job = new Job();
                  Map&lt;String, Object&gt; model = new HashMap&lt;&gt;();
                  model.put(&quot;Name&quot;,friend.name);
                  job.sendWish(friend.email,model);
              }
          });
      }
  }
  @Configuration
  @ConditionalOnProperty(name = &quot;scheduling.enabled&quot;, matchIfMissing = true)
  @EnableScheduling
  class Tasks {
  }
  //Model
  @Data
  @Setter
  @Getter
  @NoArgsConstructor
  @AllArgsConstructor
  @ToString
  @Entity
  class MyFriends {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      Long id;
      String name;
      @Email
      @Column(unique = true)
      String email;
      @JsonFormat(pattern = &quot;dd/MM/yyyy&quot;, shape = JsonFormat.Shape.STRING)
      Date dob;
      Long phoneNumber;
  }
  //Repository
  interface MyFriendsRepository extends JpaRepository&lt;MyFriends, Long&gt; {
  }
  @RestController
  @RequestMapping(&quot;/&quot;)
  class FriendsController {
      @Autowired
      MyFriendsRepository myFriendsRepository;
      @PostMapping(&quot;/friends&quot;)
      ResponseEntity&lt;MyFriends&gt; createFriend(@RequestBody MyFriends myFriend) {
          myFriendsRepository.save(myFriend);
          return new ResponseEntity&lt;MyFriends&gt;(myFriend, HttpStatus.CREATED);
      }
      @GetMapping(&quot;/friends&quot;)
      List&lt;MyFriends&gt; getFriends() {
          return myFriendsRepository.findAll();
      }
  }
  //Interface
  interface wisher{
      void sendWish(String email, Map&lt;String, Object&gt; model);
  }
  //Implementation
  @Service
  class Job implements wisher{
      @Autowired
      private JavaMailSender sender;
      @Autowired
      private freemarker.template.Configuration config;
      @Override
      public void sendWish(String email,Map&lt;String, Object&gt; model) {
          System.out.println(&quot;wishes sent to &quot;+ email);
          MimeMessage message = sender.createMimeMessage();
          try {
              MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                      StandardCharsets.UTF_8.name());
              Template template = config.getTemplate(&quot;new-template.ftl&quot;);
              String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
              helper.setTo(email);
              helper.setFrom(&quot;kondasaitej@gmail.com&quot;);
              helper.setSubject(&quot;Happy Birthday&quot;);
              helper.setText(html,true);
              sender.send(message);
          } catch (MessagingException | IOException | TemplateException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/DxTJZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DxTJZ.png" alt="Friends Table" /></a></p>
  <pre><code></code></pre>
  <h2>FreeMaker file in ./resources/Templates</h2>
  <pre><code>/**
   * FREEMAKER new-template.ftl
   * &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
   * &lt;head&gt;
   *     &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
   *     &lt;title&gt;Comment Alert Mail&lt;/title&gt;
   * &lt;/head&gt;
   * &lt;body&gt;
   * &lt;p&gt;happy birthday ${Name}&lt;/p&gt;
   * &lt;/body&gt;
   */
  </code></pre>
  <p>This is the error I'm getting this following</p>
  <pre><code>
  Hibernate: select myfriends0_.id as id1_0_, myfriends0_.dob as dob2_0_, myfriends0_.email as email3_0_, myfriends0_.name as name4_0_, myfriends0_.phone_number as phone_nu5_0_ from my_friends myfriends0_
  wishes sent to kondasaitej@protonmail.com
  2020-09-26 21:01:52.012 ERROR 84242 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
  
  java.lang.NullPointerException: null
      at dev.teja.happybirthday.Job.sendWish(HappybirthdayApplication.java:115) ~[classes/:na]
      at dev.teja.happybirthday.HappybirthdayApplication.lambda$WishMyFriends$0(HappybirthdayApplication.java:53) ~[classes/:na]
      at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_251]
      at dev.teja.happybirthday.HappybirthdayApplication.WishMyFriends(HappybirthdayApplication.java:44) ~[classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
      at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_251]
      at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_251]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_251]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_251]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_251]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_251]
      at java.lang.Thread.run(Thread.java:748) [na:1.8.0_251]
  
  </code></pre>