2015-06-10 65 views
0

我想從數據庫中檢索找到的對象或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; 
    } 
} 

}

+0

它與@Inject annotaion注入。它工作正常。我想我忘了這一個,我應該試試/抓住這個,我會在 – Kapparino

+0

之後嘗試這個,你可以發佈完整的代碼,並顯示如何填充'loggedInUser'和'project'變量?在他們中的任何一個都是空的,你可能會拋出NPE – user902383

+0

這一切都很好,期待這種方法。但我會用完整的代碼更新我的帖子。 – Kapparino

回答

1

從完整的代碼,它看起來像最好的辦法是該項目爲空,檢查完成課後,我們看到它的不注射,和其值僅在onActivate函數中設置,如果contextValue不爲null並且projectDao.getByID(contextValue)返回非空值。

能否請您確認在調用之前正確happenning到onActionFromFollowProject

+0

我可以確認,在我調用onActionFromFollowProject之前,項目不是null。 – Xerath

+0

我認爲try catch是這裏的解決方案,但我不確定我應該在DAO或Tracker頁面中驗證哪些內容? – Xerath

+0

是另一回事空,你可以調試或使用'System.out.println'檢查什麼是空,唯一的三件事情,可能會導致NPE是 - 'loggedInUser','project'和'projectFollowerDao' –