当检索现有的Mongo DB集合时,会出现null指针异常



我创建了一个带有数据的MongoDB,但当检索特定集合中的所有文档时,我的程序会出现空指针异常。

这是我的代码,UserRepository.java

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<User,String> {

}

UserSeeder.java

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<User> getAllUsers(){
List <User> allUsers = this.userRepository.findAll();
return allUsers;
}
}

User.java

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 = "users")
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<Role> roles = new HashSet<>();
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<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}

错误为,

java.lang.NullPointerException:无法调用"com.weatherdata.api.dbconnector.UserRepository.findAll(("因为";this.userRepository";为null网址:com.weatherdata.api.users.UserSeeder.getAllUsers(UserSeeder.java:13(~[classes/:na]在com.weatherdata.api.filter.Alert.isExceeded(Alert.java:20(~[classes/:na]网址:com.weatherdata.api.controller.SensorController.insert(SensorControll.java:56(~[classes/:na]位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method(~[na:na]位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64(~[na:na]位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43(~[na:na]位于java.base/java.lang.reflect.Method.ioke(Method.java:564(~[na:na]在org.springframework.web.method.support.InvocaleHandlerMethod.doInvoke(InvocableHandlerMethod.java:197(~[spring-web-5.3.2.jar:5.3.2]位于org.springframework.web.method.support.InvocaleHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141(~[spring-web-5.3.2.jar:5.3.2]网址:org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106(~[spring-webmvc-5.3.2.jar:5.3.2]网址:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894(~[spring-webmvc-5.3.2.jar:5.3.2]网址:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808(~[spring-webmvc-5.3.2.jar:5.3.2]网址:org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:87(~[spring-webmvc-5.3.2.jar:5.3.2]在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061(~[spring-webmvc-5.3.2.jar:5.3.2]位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961(~[spring-webmvc-5.3.2。jar:5.3.2]网址:org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006(~[spring-webmvc-5.3.2。jar:5.3.2]网址:org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909(~[spring-webmvc-5.3.2。jar:5.3.2]

但rest api中的thig-is相同函数运行良好

这就是我在Alert.java 中使用UserSedder的方式

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<User> userList;
@Override
public double getThreshold() {
return this.thresholdValue;
}
@Override
public boolean isExceeded(double dataValue) {
if(thresholdValue < 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;
}
}

您应该用@Service@Component注释类UserSeeder,让Spring创建并处理该类的一个实例作为Spring托管bean,这样,依赖注入应该可以正常工作,假设Spring扫描包com.weatherdata.api.users

另一种选择是在@Configuration类中定义@Bean

您可以在许多在线文章和相关文档中找到更多关于Spring中依赖注入的信息(例如链接1或链接2等(。

更新

当您手动实例化对象时,依赖项注入不起作用,您正在使用

new UserSeeder()

解决问题的一种方法是使Alert子类spring也进行管理,并使用@Autowired来实例化类型为UserSeeder的属性,从而删除new的用法。

你可以使用类似(根据需要更改(的东西

public abstract class Alert implements AlertType {
private UserSeeder userSeeder;
public Alert (UserSeeder userSeeder) {
this.userSeeder = userSeeder;
}
// change new UserSeeder() with this.userSeeder
}
@Service
public class AlertService extends Alert {
@Autowired
public AlertService(UserSeeder userSeeder) {
super(userSeeder);
}
}

您必须在具有业务逻辑的Classes上使用SterioType Annotation@Service,在对所有应用程序层通用的Classes或Utility类上使用@Component,因为当您在其底层的主类上注释@Springbootapplication时,它会自动创建basepackage下类的实例。

最新更新