Null Pointer Exception Traced to Line 90 of CommentPostgres.java, SQL 在 DBeaver 中工作正常,测试不
Null Pointer Exception Traced to Line 90 of CommentPostgres.java, SQL works correctly in DBeaver, Test does not
整个项目的所有代码都可用here
数据库是PostgreSQL 12.7
后端是 Java 11.0.12
我正在使用 JUnit 5.8.1
构建我的 TDD 测试
这是CommentDaoTest.java
None 它正在工作,但我正在专门研究 getAllNotNull
方法的第一行获取异常响应,导致 CommentPostgres.java
的第 90 行出现 NullPointerException
package com.revature.data;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.revature.beans.Comment;
import com.revature.data.postgres.CommentPostgres;
public class CommentDaoTest {
private CommentDAO cd = new CommentPostgres();
@BeforeEach
public void setup()
{
cd = new CommentPostgres();
}
@Test
public void getByIdNotNull()
{
Comment actual = cd.getById(1);
assertNotEquals(null, actual);
}
@Test
public void getByIdValidComment()
{
String expected = "Polarised methodical access";
Comment one = cd.getById(1);
String actual = one.getCommentText();
assertEquals(expected, actual);
}
@Test
public void getAllNotNull()
{
Set<Comment> actual = cd.getAll();
assertNotEquals(null, actual);
}
}
这里是CommentPostgres.java
的相关部分...
@Override
public Set<Comment> getAll() {
Set<Comment> comments = new HashSet<>();
try (Connection conn = connUtil.getConnection()) {
String sql = "select * from comment";
Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
Comment comment = new Comment();
comment.setCommentId(resultSet.getInt("comment_id"));
comment.setCommentText(resultSet.getString("comment_text"));
**Line 90** comment.getApprover().setEmpId(resultSet.getInt("approver_id"));
comment.getRequest().setReqId(resultSet.getInt("req_id"));
comment.setSentAt(resultSet.getTimestamp("sent_at").toLocalDateTime());
System.out.println(comment);
comments.add(comment);
}
} catch (SQLException e) {
e.printStackTrace();
}
return comments;
}
这里是相关的SQL
create table if not exists employee (
emp_id serial unique primary key,
first_name varchar(40),
last_name varchar(40),
username varchar(30),
passwd varchar(25),
role_id integer not null references user_role,
funds real,
supervisor_id integer,
dept_id integer
);
create table if not exists reimbursement (
req_id serial unique primary key,
emp_id integer references employee,
event_date date,
event_time time,
location varchar(50),
description varchar(75),
cost real,
grading_format_id integer references grading_format,
event_type_id integer references event_type,
status_id integer references status,
submitted_at time
);
create table if not exists comment (
comment_id serial unique primary key,
req_id integer references reimbursement,
approver_id integer references employee,
comment_text varchar(100),
sent_at time
);
INSERT INTO comment
(req_id, approver_id, comment_text, sent_at)
VALUES
(1, 43, 'Polarised methodical access', '8:56:03'),
(2, 34, 'Decentralized 3rd generation encryption', '16:26:13'),
(3, 1, 'Open-architected asymmetric firmware', '3:52:08'),
(4, 34, 'Upgradable content-based synergy', '8:01:03'),
(6, 49, 'Progressive foreground frame', '7:35:55'),
(7, 43, 'Persevering didactic definition', '16:55:07'),
(8, 43, 'Proactive responsive success', '2:21:47'),
(9, 17, 'Devolved content-based task-force', '22:04:18'),
(10, 43, 'Self-enabling client-server orchestration', '9:03:46'),
(13, 49, 'Pre-emptive stable encoding', '12:47:36'),
(14, 11, 'Streamlined asymmetric initiative', '17:45:58'),
(15, 43, 'Open-architected web-enabled leverage', '2:19:17'),
(17, 43, 'Innovative transitional alliance', '12:43:29'),
(19, 1, 'Organized didactic protocol', '3:54:43'),
(20, 17, 'Switchable 5th generation solution', '20:54:12');
包含其余部分的脚本位于顶部 link 中的 github。
当我做一个
SELECT * FROM comment;
在 DBeaver 中,我得到了所有带有 approver_id's
的评论
然而,当我 运行 getAllNotNull
时,我得到一个指向 approver_id
的 NullPointer。我放了一个 Sys.out 试图抓住评论 90 以下的几行,但它没有命中,所以 NullPointer 是第一次发生。
这是堆栈跟踪。
java.lang.NullPointerException
at com.revature.data.postgres.CommentPostgres.getAll(CommentPostgres.java:90)
at com.revature.data.CommentDaoTest.getAllNotNull(CommentDaoTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod[=16=](ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[=16=](ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute[=16=](EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
如果您想添加更多文件或代码片段,请告诉我。谢谢你的帮助。
此行创建一个新的空 Comment
对象:
Comment comment = new Comment();
在那行之后,您永远不会调用 comment.setApprover()
,因此 comment
对象中的批准者 属性 是 null
。这与数据库中丢失的数据没有任何关系,这是您初始化 Java 对象的方式的问题。
看看你在做什么:
comment.getApprover().setEmpId(resultSet.getInt("approver_id"));
你说的是“获取我刚刚创建的新评论对象,从中获取批准者对象,然后设置该批准者的 ID”。相反,您的代码需要如下所示:
Approver approver = getApproverByEmpId(resultSet.getInt("approver_id"));
comment.setApprover(approver);
其中 getApproverByEmpId(Integer empId)
是您需要创建的新方法,用于查询 employee
table 和 returns 一个 Employee
对象。
整个项目的所有代码都可用here
数据库是PostgreSQL 12.7
后端是 Java 11.0.12
我正在使用 JUnit 5.8.1
这是CommentDaoTest.java
None 它正在工作,但我正在专门研究 getAllNotNull
方法的第一行获取异常响应,导致 CommentPostgres.java
package com.revature.data;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.revature.beans.Comment;
import com.revature.data.postgres.CommentPostgres;
public class CommentDaoTest {
private CommentDAO cd = new CommentPostgres();
@BeforeEach
public void setup()
{
cd = new CommentPostgres();
}
@Test
public void getByIdNotNull()
{
Comment actual = cd.getById(1);
assertNotEquals(null, actual);
}
@Test
public void getByIdValidComment()
{
String expected = "Polarised methodical access";
Comment one = cd.getById(1);
String actual = one.getCommentText();
assertEquals(expected, actual);
}
@Test
public void getAllNotNull()
{
Set<Comment> actual = cd.getAll();
assertNotEquals(null, actual);
}
}
这里是CommentPostgres.java
的相关部分...
@Override
public Set<Comment> getAll() {
Set<Comment> comments = new HashSet<>();
try (Connection conn = connUtil.getConnection()) {
String sql = "select * from comment";
Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
Comment comment = new Comment();
comment.setCommentId(resultSet.getInt("comment_id"));
comment.setCommentText(resultSet.getString("comment_text"));
**Line 90** comment.getApprover().setEmpId(resultSet.getInt("approver_id"));
comment.getRequest().setReqId(resultSet.getInt("req_id"));
comment.setSentAt(resultSet.getTimestamp("sent_at").toLocalDateTime());
System.out.println(comment);
comments.add(comment);
}
} catch (SQLException e) {
e.printStackTrace();
}
return comments;
}
这里是相关的SQL
create table if not exists employee (
emp_id serial unique primary key,
first_name varchar(40),
last_name varchar(40),
username varchar(30),
passwd varchar(25),
role_id integer not null references user_role,
funds real,
supervisor_id integer,
dept_id integer
);
create table if not exists reimbursement (
req_id serial unique primary key,
emp_id integer references employee,
event_date date,
event_time time,
location varchar(50),
description varchar(75),
cost real,
grading_format_id integer references grading_format,
event_type_id integer references event_type,
status_id integer references status,
submitted_at time
);
create table if not exists comment (
comment_id serial unique primary key,
req_id integer references reimbursement,
approver_id integer references employee,
comment_text varchar(100),
sent_at time
);
INSERT INTO comment
(req_id, approver_id, comment_text, sent_at)
VALUES
(1, 43, 'Polarised methodical access', '8:56:03'),
(2, 34, 'Decentralized 3rd generation encryption', '16:26:13'),
(3, 1, 'Open-architected asymmetric firmware', '3:52:08'),
(4, 34, 'Upgradable content-based synergy', '8:01:03'),
(6, 49, 'Progressive foreground frame', '7:35:55'),
(7, 43, 'Persevering didactic definition', '16:55:07'),
(8, 43, 'Proactive responsive success', '2:21:47'),
(9, 17, 'Devolved content-based task-force', '22:04:18'),
(10, 43, 'Self-enabling client-server orchestration', '9:03:46'),
(13, 49, 'Pre-emptive stable encoding', '12:47:36'),
(14, 11, 'Streamlined asymmetric initiative', '17:45:58'),
(15, 43, 'Open-architected web-enabled leverage', '2:19:17'),
(17, 43, 'Innovative transitional alliance', '12:43:29'),
(19, 1, 'Organized didactic protocol', '3:54:43'),
(20, 17, 'Switchable 5th generation solution', '20:54:12');
包含其余部分的脚本位于顶部 link 中的 github。
当我做一个
SELECT * FROM comment;
在 DBeaver 中,我得到了所有带有 approver_id's
的评论
然而,当我 运行 getAllNotNull
时,我得到一个指向 approver_id
的 NullPointer。我放了一个 Sys.out 试图抓住评论 90 以下的几行,但它没有命中,所以 NullPointer 是第一次发生。
这是堆栈跟踪。
java.lang.NullPointerException
at com.revature.data.postgres.CommentPostgres.getAll(CommentPostgres.java:90)
at com.revature.data.CommentDaoTest.getAllNotNull(CommentDaoTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod[=16=](ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[=16=](ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute[=16=](EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
如果您想添加更多文件或代码片段,请告诉我。谢谢你的帮助。
此行创建一个新的空 Comment
对象:
Comment comment = new Comment();
在那行之后,您永远不会调用 comment.setApprover()
,因此 comment
对象中的批准者 属性 是 null
。这与数据库中丢失的数据没有任何关系,这是您初始化 Java 对象的方式的问题。
看看你在做什么:
comment.getApprover().setEmpId(resultSet.getInt("approver_id"));
你说的是“获取我刚刚创建的新评论对象,从中获取批准者对象,然后设置该批准者的 ID”。相反,您的代码需要如下所示:
Approver approver = getApproverByEmpId(resultSet.getInt("approver_id"));
comment.setApprover(approver);
其中 getApproverByEmpId(Integer empId)
是您需要创建的新方法,用于查询 employee
table 和 returns 一个 Employee
对象。