title: When retrieving existing Mongo DB Collection gives null pointer exception
body: <p>I have a created MongoDB with data, But when retrieving all documents in a specific collection my program gives a null pointer exception.</p>
  <p>Here is my code,
  <strong>UserRepository.java</strong></p>
  <pre><code>package com.weatherdata.api.dbconnector;
  
  import com.weatherdata.api.users.User;
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @Repository
  public interface UserRepository extends MongoRepository&lt;User,String&gt; {
      
  }
  
  
  </code></pre>
  <p><strong>UserSeeder.java</strong></p>
  <pre><code>package com.weatherdata.api.users;
  
  import com.weatherdata.api.dbconnector.UserRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import java.util.List;
  
  public class UserSeeder {
      @Autowired
      UserRepository userRepository;
  
      public List&lt;User&gt; getAllUsers(){
          List &lt;User&gt; allUsers = this.userRepository.findAll();
          return allUsers;
      }
  }
  
  </code></pre>
  <p><strong>User.java</strong></p>
  <pre><code>package com.weatherdata.api.users;
  
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.DBRef;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  import java.util.HashSet;
  import java.util.Set;
  @Document(collection = &quot;users&quot;)
  public class User {
      @Id
      private String id;
      private String username;
      private String email;
      private String mobile;
      private String method;
      private String password;
  
      @DBRef
      private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
  
      public User() {
      }
  
      public User(String username, String email,String mobile,String method, String password) {
          this.username = username;
          this.email = email;
          this.mobile = mobile;
          this.method = method;
          this.password = password;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getMobile() {
          return mobile;
      }
  
      public void setMobile(String mobile) {
          this.mobile = mobile;
      }
  
      public String getMethod() {
          return method;
      }
  
      public void setMethod(String method) {
          this.method = method;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public Set&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(Set&lt;Role&gt; roles) {
          this.roles = roles;
      }
  }
  
  
  </code></pre>
  <p><strong>Error is,</strong></p>
  <blockquote>
  <p>java.lang.NullPointerException: Cannot invoke &quot;com.weatherdata.api.dbconnector.UserRepository.findAll()&quot; because &quot;this.userRepository&quot; is null
  at com.weatherdata.api.users.UserSeeder.getAllUsers(UserSeeder.java:13) ~[classes/:na]
  at com.weatherdata.api.filter.Alert.isExceeded(Alert.java:20) ~[classes/:na]
  at com.weatherdata.api.controller.SensorController.insert(SensorController.java:56) ~[classes/:na]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[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]
  at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.2.jar:5.3.2]
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.2.jar:5.3.2]
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.2.jar:5.3.2]</p>
  </blockquote>
  <p><strong>But the thig is same function in rest api is working finely.</strong></p>
  <p>This is how I use UserSedder in Alert.java</p>
  <pre><code>package com.weatherdata.api.filter;
  
  import com.weatherdata.api.users.User;
  import com.weatherdata.api.users.UserSeeder;
  
  import java.util.List;
  
  public abstract class Alert implements AlertType {
      private double thresholdValue;
      private List&lt;User&gt; userList;
  
      @Override
      public double getThreshold() {
          return this.thresholdValue;
      }
  
      @Override
      public boolean isExceeded(double dataValue) {
          if(thresholdValue &lt; dataValue){
              userList = new UserSeeder().getAllUsers();
              System.out.println(userList.stream().count());
              return true;
          }
          else{
              return false;
          }
      }
  
      public double getThresholdValue() {
          return thresholdValue;
      }
  
      public void setThresholdValue(double thresholdValue) {
          this.thresholdValue = thresholdValue;
      }
  }
  
  </code></pre>