0

I wanted to retrieve found object or a null from a database.

In my ProjectFollowerImpl, for a method useFollowingProject I return a single instance that matches the query, or null if the query returns no results.

@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();
}

Then in a tapestry page, I have following methods:

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

However, tapestry throws null pointer exception, stack trace:

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

So why is it a problem to return a null value for a object? What am I doing wrong ?

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

}

6
  • It is injected with @Inject annotaion. It works fine. I think I forgot this one, I should maybe try/catch this, I will try this in a moment
    – Kapparino
    Jun 10, 2015 at 9:13
  • can you post completed code, and show how you populate loggedInUser and project variables? in any of them is null you might have NPE thrown
    – user902383
    Jun 10, 2015 at 9:13
  • It all works fine, expect this method. But I will update my post now with complete code.
    – Kapparino
    Jun 10, 2015 at 9:15
  • Can you please advice what is the variable - projectFollowerDao ? Seems like that is null. Jun 10, 2015 at 9:17
  • @AnandSKumar It's is injected in declaration. It is not null.
    – Xerath
    Jun 10, 2015 at 9:24

1 Answer 1

1

From the complete code, it looks like the best bet is that project is null, after checking complete class, we see that its not injected, and its value is only set in onActivate function, if contextValue is not null and projectDao.getByID(contextValue) returns a non-null value.

Could you please confirm that is happenning correctly before the call to onActionFromFollowProject

12
  • I can confirm that, project is not null before I call onActionFromFollowProject.
    – Xerath
    Jun 10, 2015 at 9:31
  • I think try catch is the solution here, but I'm not sure where should I validate, in DAO or in Tracker page ?
    – Xerath
    Jun 10, 2015 at 9:32
  • Is something else null, can you debug or use System.out.println to check what is null , the only three things that can cause NPE are - loggedInUser , project and projectFollowerDao Jun 10, 2015 at 9:32
  • Also , don't you need a getter/setter for the injected properties ? Jun 10, 2015 at 9:34
  • No, bcs I'm using @Property.
    – Xerath
    Jun 10, 2015 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.