title: How to calculate sum from in Spring Boot JPA repository
body: <p>I'm working on a Spring Boot application and I have encountered the following challenge when trying to find the SUM. I have tried using JPQL and JPA syntax, but I'm still getting a null pointer exception.</p>
  <pre class="lang-java prettyprint-override"><code>@Getter
  @Setter
  @NoArgsConstructor
  @Entity
  public class ReportData extends Auditable {
      @Id
      @GeneratedValue (strategy = GenerationType.IDENTITY)
      private Long reportId;
      private Integer tests;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface reportRepository  extends JpaRepository &lt;ReportData, Long&gt;  {
  // or &quot;SELECT SUM(m.tests) FROM ReportData m&quot; 
    @Query(value = &quot;SELECT SUM(tests) FROM ReportData&quot;, nativeQuery = true)
    int totalTest ();
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@GetMapping(value = &quot;/reports&quot;)
  public String statisticsPage (Model model){
     model.addAttribute(&quot;tests&quot;, reportRepository.totalTest());
     return (&quot;statistics&quot;);
  }
  </code></pre>
  <p>The error I get is:<a href="https://i.stack.imgur.com/xMHXG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xMHXG.png" alt="its a null pointer exception" /></a></p>