title: 500 Internal Server Error; when using POST method in springBoot rest api
body: <p>I used Spring Boot, <code>POST</code> method for create a new score for my player.
  In <code>POST</code> method, I check if the player and game exists then create new score and also add score and its related date into the history of my score's class.
  Each score has history which contains score and its date. the history has type list of History's class</p>
  
  <p>The History Class:</p>
  
  <pre><code>package thesisMongoProject;
  
  import java.util.Date;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  @Document(collection = "history")
  public class History {
  
          @Id
          private String score;
          private Date date;
  
          public History(String score, Date date) {
              super();
              this.score = score;
              this.date = date;
          }
  
          public String getScore() {
              return score;
          }
  
          public void setScore(String score) {
              this.score = score;
          }
  
          public Date getDate() {
              return date;
          }
  
          public void setDate(Date date) {
              this.date = date;
          }
  
          @Override
          public String toString() {
              return "History [score=" + score + ", date=" + date + "]";
          }
  
  }
  </code></pre>
  
  <p>The Score Class:</p>
  
  <pre><code>package thesisMongoProject;
  
  import java.util.Date;
  import java.util.List;
  import javax.validation.constraints.NotBlank;
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  import com.fasterxml.jackson.annotation.JsonView;
  
  @Document(collection = "score")
  public class Score {
  
      @Id
      @NotBlank
      @JsonView(Views.class)
      private String score;
      @NotBlank
      @JsonView(Views.class)
      private String player;
      @NotBlank
      @JsonView(Views.class)
      private String code;
      @JsonView(Views.class)
      private Date date;
      private List&lt;History&gt; history;
  
      public Score(@NotBlank String score, String player, String code, List&lt;History&gt; history, Date date) {
          super();
          this.score = score;
          this.player = player;
          this.code = code;
          this.history = history;
          this.date = date;
      }
      public String getScore() {
          return score;
      }
      public void setScore(String score) {
          this.score = score;
      }
      public String getPlayer() {
          return player;
      }
      public void setPlayer(String player) {
          this.player = player;
      }
      public String getCode() {
          return code;
      }
      public void setCode(String code) {
          this.code = code;
      }
      public List&lt;History&gt; getHistory() {
          return history;
      }
      public void setHistory(List&lt;History&gt; history) {
          this.history = history;
      }
      public Date getDate() {
          return date;
      }
      public void setDate(Date date) {
          this.date = date;
      }
      @Override
      public String toString() {
          return "Score [score=" + score + ", player=" + player + ", code=" + code + ", history=" + history + ", date="
                  + date + "]";
      }
  
  }
  </code></pre>
  
  <p>And the POST method:</p>
  
  <pre><code>@RestController
  @RequestMapping("/score")
  public class ScoreController {
      @Autowired
      private ScoreRepository srepo;
      @Autowired
      private PlayerRepository prepo;
      @Autowired
      private GamesRepository grepo;
      @Autowired
      private HistoryRepository hrepo;
      private List&lt;History&gt; history;
      private History h = null;
  
      //Create Score
          @PostMapping
          public ResponseEntity&lt;?&gt; createScore(@RequestBody @JsonView(Views.class) @Valid  Score score) {
              //check player exist
              Player p = prepo.findByNickname(score.getPlayer());
              //check game's cod exist
              Games g = grepo.findByCode(score.getCode());
              //check score exist
              Score s = srepo.findByScore(score.getScore());
               // = hrepo.findByScore(score.getScore());
              if(s != null)
              {
                  return ResponseEntity.status(409).body("Conflict!!");
              }else if((p != null) &amp;&amp; (g != null)) {
                  h.setScore(score.getScore());
                  h.setDate(score.getDate());
                  hrepo.save(h);
                  history.add(h);
                  //history.add(hrepo.findById(score.getScore()).get());
                  score.setHistory(history);
                  srepo.save(score);
  
                  return ResponseEntity.status(201).body("Created!"); 
              }
              else {
                  return ResponseEntity.status(400).body("Bad Request!");
              }
  
          }
  </code></pre>
  
  <p>In my <code>POST</code> method I tried to <code>setScore</code> and <code>setDate</code> for an object of History class, and then I saved them by <code>hrepo</code> which is history Repository and then I added this to the history variable  of type <code>List&lt;History&gt;</code>, after that I <code>setHistory</code> of my score class with <code>srepo</code>, Score repository . But when I execute my program, in PostMan I have <code>500 Internal Server Error</code> and in the console I have this Error:</p>
  
  <pre><code>java.lang.NullPointerException: null
      at thesisMongoProject.controller.ScoreController.createScore(ScoreController.java:63) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
  </code></pre>
  
  <p>which is exactly the line that I <code>setScore</code> of object h, <code>h.setScore(score.getScore());</code> 
  I cannot understand what is my mistake.</p>