提问人:Tsai Hsuan Hsieh 提问时间:7/6/2023 最后编辑:Tsai Hsuan Hsieh 更新时间:7/6/2023 访问量:52
Entity getter 方法中的 Null
Null in Entity getter method
问:
我有这个 NullPointerException 运行时异常,所以我怀疑是我在实体中的 getter 方法不起作用。但我不确定,我不知道如何解决它。如果可以的话,请纠正我。
我现在使用 Java 作为后端来接受前端请求。
一个。我找了 1。官方文件:https://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html, 2. 如何解决java.lang.NullPointerException错误?3. 调用 getter 方法时出现 NullPointerException
B.我期待看到 user.getUsername() 和 opinion.getCollaboratorName() 的打印结果,但我只得到了前一个。我不清楚“opinion.getCollaboratorName() is null”。
- 这是我的控制器:
@RestController
@RequestMapping("/forum"){
private final JwtDecoder jwtDecoder;
private final UserServiceImpl userServiceImpl;
private final OpinionsRepository opinionsRepository;
@Autowired
//constructor
public OpinionsController(JwtDecoder jwtDecoder, UserServiceImpl userServiceImpl, OpinionsRepository opinionsRepository) {
this.jwtDecoder = jwtDecoder;
this.userServiceImpl = userServiceImpl;
this.opinionsRepository = opinionsRepository;
}
@PutMapping("/opinions")
public ResponseEntity<?> updateOpinion(**@RequestBody Opinion opinion**, @CookieValue(name = "dashboardToken", required = true) String dashboardToken, HttpServletResponse response) {
System.out.println("Java 3 at putmapping/opinions to edit individual posts,outside try, {id}" + **opinion.toString()**);
try {
System.out.println("Printing out the @RequestBody Opinion");
**System.out.println(opinion);**
System.out.println("Java 4 inside try put mapping");
String username = jwtDecoder.decodeUserInfoFromDashboardToken(dashboardToken);
System.out.println("email in PutMapping Opinions:" + username);
System.out.println("OpinionID in PutOpinions id:");
System.out.println(opinion.getId());
User user = userServiceImpl.findUserByEmail(email); //User is an Entity
**System.out.println("opinion.getCollaboratorName()");
System.out.println(opinion.getCollaboratorName());**//how is this possible??
if (!Objects.equals(user.getUsername(), opinion.getCollaboratorName())) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("The Collaborator does not correspond with the author of the article in the Database. Please contact us.");
}
response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept");
//response.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
//response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Expose-Headers", "loginToken");
response.addHeader("Access-Control-Max-Age", "3600");
System.out.println("Hey!!!This is the response from putmapping,process almost done:" + response.toString());
return new ResponseEntity<Opinion>(opinionsRepository.save(opinion), HttpStatus.OK);
} catch (UserNotFoundException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("The Collaborator does not correspond with the author of the article in the Database. Please contact us.");
} catch (Exception e) {
System.out.println("Oops, something went wrong. at getmapping opinions line 55");
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Collections.emptyList());
}
}
}
- 这是我的实体,叫做“意见”
@Entity
@Table(name="opinion")
public class Opinion {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@Column(name="collaborator_name",nullable=false)
private String collaboratorName;
@Column(nullable=false)
private String title;
@Column(nullable=false)
private String body;
@Column(nullable=false)
private String category;
@Column(name="updated_at")
@UpdateTimestamp
private Date updatedAt;
@Column(name="created_at",nullable=false,updatable=false)
@CreationTimestamp
private Date createdAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setCollaboratorName(String collaboratorName) {
this.collaboratorName = collaboratorName;
}
** public String getCollaboratorName() {
return collaboratorName;
}**
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return "Opinion{" +
"id=" + id +
", collaboratorName='" + collaboratorName + '\'' +
", title='" + title + '\'' +
", body='" + body + '\'' +
", category='" + category + '\'' +
", updatedAt=" + updatedAt +
", createdAt=" + createdAt +
'}';
}
}
- 这是 Java 控制台显示的内容:
Java 3 at putmapping/opinions to edit individual posts,outside try, {id}Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Printing out the @RequestBody Opinion
Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Java 4 inside try put mapping
Hi This is from decodeUserInfoFromDashboardToken in JwtDecoder, and the Claims are:
{sub=Stasy, opinionsList=[{id=8, **collaboratorName=Stasy**, title=Hi there, body=Opps I hope it wrks, category=gender equality, updatedAt=1688451552464, createdAt=1688451552464}, {id=9, collaboratorName=Stasy, title=Now Tuesday, body=Hi there, TUesday again, category=gender equality, updatedAt=1688451787036, createdAt=1688451787036}, {id=10, collaboratorName=Stasy, title=11th Revolution, body=I hope this time it works, category=gender equality, updatedAt=1688452876627, createdAt=1688452876627}], [email protected], password=1128, username=Stasy, iat=1688628751}
**result from getUserInfoFromLoginToken:Stasy
username in PutMapping Opinions:Stasy**
OpinionID in PutOpinions id:
8
Hibernate: select u1_0.userid,u1_0.email,u1_0.password,u1_0.username from users u1_0 where u1_0.email=?
**opinion.getCollaboratorName()
null**
答: 暂无答案
评论