我想從數據庫中檢索找到的對象或null。實體不能爲空?
在我ProjectFollowerImpl,爲useFollowingProject我返回如果查詢不返回任何結果相匹配的查詢,或空單實例的方法。
@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();
}
在掛毯頁
然後,我有以下方法:
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);
}
}
然而,掛毯拋出空指針異常,堆棧跟蹤:
com.rile.issuetracker.pages.Tracker getUserFollowingProject() Tracker.java 75
com.rile.issuetracker.pages.Tracker advised$onActionFromFollowProject_6d8b9673baf() Tracker.java 80
那麼,爲什麼它是一個問題返回一個空值的對象?我究竟做錯了什麼 ?
UPDATE:
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;
}
}
}
它與@Inject annotaion注入。它工作正常。我想我忘了這一個,我應該試試/抓住這個,我會在 – Kapparino
之後嘗試這個,你可以發佈完整的代碼,並顯示如何填充'loggedInUser'和'project'變量?在他們中的任何一個都是空的,你可能會拋出NPE – user902383
這一切都很好,期待這種方法。但我會用完整的代碼更新我的帖子。 – Kapparino