gpt4 book ai didi

java - 实体不能为空?

转载 作者:行者123 更新时间:2023-12-01 11:26:52 25 4
gpt4 key购买 nike

我想从数据库中检索找到的对象或空值。

在我的 ProjectFollowerImpl 中,对于 useFollowingProject 方法,我返回与查询匹配的单个实例,如果查询未返回结果,则返回 null。

@Override
public ProjectFollower userFollowingProject(Integer userId, Integer projectId) {
return (ProjectFollower) session.createCriteria(classType)
.add(Restrictions.eq("user.id", userId))
.add(Restrictions.eq("project.id", projectId))
.uniqueResult();
}

然后在 Tapestry 页面中,我有以下方法:

public ProjectFollower getUserFollowingProject() {
return projectFollowerDao.userFollowingProject(loggedInUser.getId(), project.getId());
}

@CommitAfter
void onActionFromFollowProject() {
ProjectFollower pf = getUserFollowingProject();
if (pf != null) {
projectFollowerDao.delete(pf.getId());
} else {
pf = new ProjectFollower();
pf.setProjectId(project);
pf.setUserId(loggedInUser);
projectFollowerDao.merge(pf);
}
}

但是,tapestry 抛出空指针异常,堆栈跟踪:

com.rile.issuetracker.pages.Tracker getUserFollowingProject()   Tracker.java    75
com.rile.issuetracker.pages.Tracker advised$onActionFromFollowProject_6d8b9673baf() Tracker.java 80

那么为什么对象返回空值会出现问题呢?我做错了什么?

更新:

public class Tracker {

@Property
@SessionState
private User loggedInUser;

@Property
@Inject
private ProjectDao projectDao;
@Property
private Project projectP1, project;
@Property
private List<Project> projectList;
@Property
@Inject
private ProjectFollowerDao projectFollowerDao;

@Property
@Inject
private TicketDao ticketDao;
@Property
private List<Ticket> ticketList;
@Property
private Ticket ticketP1;
@Property
@Inject
private TicketFollowerDao ticketFollowerDao;

@Property
private Util util = new Util();

public boolean getLoggedIn() {
return loggedInUser.getEmail() != null;
}

@PageLoaded
void onPageLoad() {
projectList = projectDao.loadAll();
ticketList = ticketDao.loadAll();
}

void onActivate(Integer contextValue) {
if (contextValue != null) {
project = projectDao.getByID(contextValue);
}
if (project != null) {
List ticketListByProjectID = ticketDao.getTicketsByProjectID(project.getId());
if (!ticketListByProjectID.isEmpty()) {
ticketList = ticketListByProjectID;
} else {
ticketList = null;
}
}
}

public ProjectFollower getUserFollowingProject() {
return projectFollowerDao.userFollowingProject(loggedInUser.getId(), project.getId());

}

@CommitAfter
void onActionFromFollowProject() {
ProjectFollower pf = getUserFollowingProject();
if (pf != null) {
projectFollowerDao.delete(pf.getId());
} else {
pf = new ProjectFollower();
pf.setProjectId(project);
pf.setUserId(loggedInUser);
projectFollowerDao.merge(pf);
}
}

public boolean getIsUserFollowingTicket() {
return ticketFollowerDao.isUserFollowingTicket(loggedInUser.getId(), ticketP1.getId());
}

@CommitAfter
void onActionFromFollowTicket() {}

public String getActiveFor(String parameter) {
if (parameter == null || parameter.isEmpty()) {
return null;
}
switch (parameter) {
case "userFollowingProject":
return getUserFollowingProject() != null ? "active" : "null";
case "userFollowingTicket":
return getIsUserFollowingTicket() ? "anchor-active" : "anchor-inactive";
default:
return null;
}
}

}

最佳答案

从完整的代码来看,最好的选择是project为null,在检查完整的类后,我们发现它没有被注入(inject),并且它的值仅在onActivate函数中设置,如果contextValue 不为 null,并且 projectDao.getByID(contextValue) 返回一个非 null 值。

能否请您在调用 onActionFromFollowProject 之前确认是否正确发生

关于java - 实体不能为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com