实体不能为空?

时间:2015-06-10 09:08:09

标签: java tapestry

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

在我的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();
}

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

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;
    }
}

}

1 个答案:

答案 0 :(得分:1)

从完整的代码看,最好的选择是项目为null,在检查完整的类之后,我们看到它没有注入,并且它的值仅在onActivate函数中设置,如果contextValue不是null和projectDao.getByID(contextValue)返回非空值。

请您在致电onActionFromFollowProject

之前确认是否正确发生