提问人:Murrchalkina 提问时间:9/29/2022 更新时间:9/29/2022 访问量:493
Servlet 上 Spring 项目中 JpaRepository 的 NullPointerException [duplicate]
NullPointerException to JpaRepository in Spring project on Servlets [duplicate]
问:
我有一个实体:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String login;
private String password;
public User() {
}
public User(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
我创建了一个带有注释@Repository的界面,如下所示:
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
因此,我需要在我的数据库中保存新用户。但是,如果我在@Service类中使用@Autowired UserRepository 并调用“save”方法,它会向我抛出错误 505 并说明: HTTP 状态 500 – 内部服务器错误 类型异常报告
消息:无法调用“com.example.testservletjsp.repository.UserRepository.save(Object)”,因为“this.userRepository”为空
说明:服务器遇到意外情况,无法满足请求。
例外
java.lang.NullPointerException:无法调用“com.example.testservletjsp.repository.UserRepository.save(Object)”,因为“this.userRepository”为空 com.example.testservletjsp.account.UserService.saveUser(UserService.java:28) com.example.testservletjsp.servlets.ExplorerServlet.doPost(ExplorerServlet.java:68) javax.servlet.http.HttpServlet.service(HttpServlet.java:682) javax.servlet.http.HttpServlet.service(HttpServlet.java:765) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 注意:服务器日志中提供了根本原因的完整堆栈跟踪。
我的 pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-servlet-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test-servlet-jsp</name>
<description>test-servlet-jsp</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
我该怎么办..?
答:
由于我没有你的项目的全部,我看不出是否有一些配置错误的类,但你总是可以访问这个(https://spring.io/guides/gs/accessing-data-mysql/)春季指南,它教你所有你必须记住的细节,以便使该项目正常工作。
评论