Getting NullPointerException with findUserNames from UserSearchService

Azhak Anwar November 28, 2024

Asking the question here as the developer community is not letting me post there. 

--

 

Good Day!

I'm following a tutorial to develop a Jira Plugin but am stuck at the REST API and getting a NullPointerException.
I am trying to search for users with the UserSearchService interface and its method findUserNames(), but I’m getting the exception.

Any help to avoid this exception and get the users is much appreciated! 

Following is my code snippet from the rest directory:

package io.codeclou.kitchen.duty.rest;
import io.codeclou.kitchen.duty.config.MyPluginJavaConfig;
...
@Named
@Path("/user")
public class UserSearchResource {
    public MyPluginJavaConfig myPluginJavaConfig;
    @Autowired
    public UserSearchResource(MyPluginJavaConfig myPluginJavaConfig) {
        this.myPluginJavaConfig = myPluginJavaConfig;
    }
    @GET
    @Path("/search")
    @Produces({MediaType.APPLICATION_JSON})
    public Response searchUsers(@QueryParam("query") final String userQuery,
                                @Context HttpServletRequest request) {
        List<UserSearchResourceModel> users = findUsers(userQuery);
        return Response.ok(users).build();
    }
    private List<UserSearchResourceModel> findUsers(String query) {
        List<UserSearchResourceModel> userSearchResourceModels = new ArrayList<>();
        UserSearchParams searchParams = UserSearchParams.builder()
            .includeActive(true)
            .sorted(true)
            .build();
        List<String> users = this.myPluginJavaConfig.userSearchService.findUserNames(query, searchParams);
        if (users != null) {
            for (String user : users) {
                userSearchResourceModels.add(new UserSearchResourceModel(user, user));
            }
        }
        return userSearchResourceModels;
    }
}

Config File: 

package io.codeclou.kitchen.duty.config;

import com.atlassian.jira.bc.user.search.UserSearchService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
...

@Configuration
@Import({
    ModuleFactoryBean.class,
    PluginAccessorBean.class
})

public class MyPluginJavaConfig {
    @Bean
    public ApplicationProperties applicationProperties() {
        return importOsgiService(ApplicationProperties.class);
    }

    @
Bean
    public MyPluginComponent myPluginComponent(ApplicationProperties applicationProperties) {
        return new MyPluginComponentImpl(applicationProperties);
    }

    @
Bean
   
public FactoryBean<ServiceRegistration> registerMyDelegatingService(
        final MyPluginComponent mypluginComponent) {
        return exportOsgiService(mypluginComponent, null, MyPluginComponent.class);
    }
    @ComponentImport
    public UserSearchService userSearchService;
}

Logs:

[INFO] [talledLocalContainer] Nov 28, 2024 4:02:08 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
[INFO] [talledLocalContainer] INFO: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
[INFO] [talledLocalContainer] Nov 28, 2024 4:02:08 PM com.sun.jersey.api.wadl.config.WadlGeneratorLoader loadWadlGenerator
[INFO] [talledLocalContainer] INFO: Loading wadlGenerator com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
[INFO] [talledLocalContainer] Nov 28, 2024 4:02:08 PM com.sun.jersey.api.wadl.config.WadlGeneratorLoader loadWadlGenerator
[INFO] [talledLocalContainer] INFO: Loading wadlGenerator com.atlassian.plugins.rest.doclet.generators.grammars.WadlGrammarsAdaptor
[INFO] [talledLocalContainer] Nov 28, 2024 4:02:08 PM com.sun.jersey.api.wadl.config.WadlGeneratorLoader loadWadlGenerator
[INFO] [talledLocalContainer] INFO: Loading wadlGenerator com.atlassian.plugins.rest.doclet.generators.resourcedoc.AtlassianWadlGeneratorResourceDocSupport
[INFO] [talledLocalContainer] 2024-11-28 16:02:11,644 http-nio-2990-exec-2 ERROR admin 962x12382x1 11mu90j 0:0:0:0:0:0:0:1 /rest/kitchenduty/1.0/user/search [c.a.p.r.c.error.jersey.ThrowableExceptionMapper] Uncaught exception thrown by REST service: null
[INFO] [talledLocalContainer] java.lang.NullPointerException
[INFO] [talledLocalContainer]   at io.codeclou.kitchen.duty.rest.UserSearchResource.findUsers(UserSearchResource.java:68)
[INFO] [talledLocalContainer]   at io.codeclou.kitchen.duty.rest.UserSearchResource.searchUsers(UserSearchResource.java:58)

 

1 answer

0 votes
vikram
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 1, 2024

Hi @Azhak Anwar 

I suspect some null user might be there, please check the users table from database. (let me know if it helped) 

try to check the below knowledge base url, might be helpful.

https://confluence.atlassian.com/jirakb/nullpointerexception-when-trying-to-access-the-user-browser-204053369.html

with Regards

Vikram P 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events