Getting NullPointerException for searchService.search

I’m trying to get all issues assigned to a particular user. I do the following for that purpose:

    ApplicationUser user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
    JqlClauseBuilder builder= JqlQueryBuilder.newClauseBuilder();
    Query query=builder.assigneeUser(user.getName()).buildQuery();
    if (searchService!=null)
        System.out.println("Search service is not null");
    else
        System.out.println("Search service is null");
    PagerFilter pagerFilter = PagerFilter.getUnlimitedFilter();
    SearchResults results=searchService.search(user,query,pagerFilter);//Getting NullPointerException at this point

I’ve AutoWired the SearchService and make sure that it’s not null before invoking the search method on it. What could be the reason for that?

If you’re Autowiring SearchService - can’t you autowire JiraAuthenticationContext ? (better performance if you can).

Is the user object null?

It’s very strange that when passing a hard coded string instead of the user name of the current user that I got from

ApplicationUser user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

I stopped getting that exception.

Is the user object null?

Is it possible?

And, why do you suggest I autowire JiraAuthenticationContext? Can it perform issue searching?

Sorry, @daniel. I misunderstood your qeustion regarding JiraAuthenticationContext. Now I get what you mean.

I missed the builder.assigneeUser( user.getName() ) part. Could you do something like assigneeIsCurrentUser() instead?

As far the injecting the JiraAuthenticationContext - it’s better for mocking as well as it helps the performance of the osgi system (instead of doing the osgi objects at runtime in your code, it happens when further up the change. ComponentAccessor(and it’s friends) should really only be used in a worst case scenario where you can’t communicate with Osgi. (Just my opinion :slight_smile: ).

1 Like