【发布时间】:2021-04-03 04:16:52
【问题描述】:
我创建了一个带有数据的 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”为空 在 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(SensorController.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.invoke(Method.java:564) ~[na:na] 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.2.jar:5.3.2] 在 org.springframework.web.method.support.InvocableHandlerMethod.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(AbstractHandlerMethodAdapter.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中的相同功能工作正常。
这就是我在 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;
}
}
【问题讨论】:
标签: java spring spring-boot