NullPointerException in resolverFactory.getServiceResourceResolver(param)

Avatar

Avatar

azus_Khan

Avatar

azus_Khan

azus_Khan

25-01-2019

I am trying to access page properties using services in AEM 6.4

I have created  an interface,

public interface AccessPageProp {

public String getPageProp();

}

I have created a class that implements this interface,

@Component(service = AcccessPagePropImpl.class, immediate = true)

public class AcccessPagePropImpl implements AccessPageProp {

Logger logger = LoggerFactory.getLogger(AcccessPagePropImpl.class);

@Reference

private ResourceResolverFactory resolverFactory;

@Override

public String getPageProp() {

logger.info("Entering into getPageProp()...");

String title = "";

Map<String, Object> param = new HashMap<String, Object>();

param.put(ResourceResolverFactory.SUBSERVICE, "readService");

ResourceResolver resourceResolver = null;

try {

resourceResolver = resolverFactory.getServiceResourceResolver(param);

Resource pageResource = resourceResolver.getResource("/content/MyProject/en/TestPageName/jcr:content");

Page myPage = pageResource.adaptTo(Page.class);

title = myPage.getTitle();

return title;

} catch (Exception e) {

logger.info("Exception...."+e.toString());

e.printStackTrace();

}

return title;

}

}

I am getting "java.lang.NullPointerException" in line "resourceResolver = resolverFactory.getServiceResourceResolver(param);"

Replies

Avatar

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

22-05-2019

karl515-qd9WHG

    Are your service user available for the bundle ? Do you get any error in the logs ?

Thanks

Veena

Avatar

Avatar

karl515-YhciiQ

Avatar

karl515-YhciiQ

karl515-YhciiQ

27-05-2019

I already have an alternative.

by using

          SlingHttpServletRequest request;

          request.getResourceResolver().getResource(location);

But I believe I will run into that service method someday. Hope I will not get the error by then.

Thanks!