resourceresolver null java.lang.NullPointerException | Community
Skip to main content
Karl515
Level 5
May 15, 2019
Solved

resourceresolver null java.lang.NullPointerException

  • May 15, 2019
  • 4 replies
  • 6722 views

I'm getting java.lang.NullPointerException

Here's my code

package com.adobe.aem.xxx.screens.core;

import java.util.HashMap;

import java.util.Map;

import javax.annotation.PostConstruct;

//Sling Imports

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.models.annotations.Model;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Component(immediate=true)

@Model(adaptables = Resource.class)

public class DisplayLocation{

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

   

    private String locationTitle;

    private String srcLocation;

    private String title = "No data";

    private String location = "/content/screens/xxx-screens/locations/xxx/xxx-displays/jcr:content";

   

    //Inject a Sling ResourceResolverFactory

    @Reference

    private ResourceResolverFactory resolverFactory;

   

    @PostConstruct

    protected void init() {

        logger.info("Post Construct Entrance");

       

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

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

        ResourceResolver resourceResolver = null;

        try {

            logger.info("ResourceResolver");

            logger.info("RF " + resolverFactory);

            title = param.toString();

            resourceResolver = resolverFactory.getServiceResourceResolver(param);   //this is the error. It then proceed to catch.

            logger.info(resourceResolver.getUserID());                                                       //lines from here isn't read.

            logger.info("Resource");

            Resource resource = resourceResolver.getResource(location);

            logger.info("ValueMap");

            ValueMap properties = resource.adaptTo(ValueMap.class);

           

            title = properties.get("sling:resourceType", String.class);

           

           

            resourceResolver.close();

        } catch (Exception e) {

            e.printStackTrace();

            logger.error("Error in Try Catch " + e);

        }

       

        locationTitle = title;

        srcLocation = location;

       

    }

   

    public String getLocationTitle() {

        return locationTitle;

    }

    public String getLocationTitle2() {

        return srcLocation;

    }

}

I've searched and searched but to no avail.

Apache Sling Service Mapper: (tried both)(and many more)

my system-user

Bundle Name:

Why do I get?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

See this where this use case works fine.

Adobe Experience Manager Help | Querying Adobe Experience Manager 6.4 JCR data using the QueryBuilder API

4 replies

Peter_Puzanovs
Community Advisor
Community Advisor
May 15, 2019

Hey Karl,

Shouldn't you be using @OSGiService for your ResourceResolverFactory, instead of @Reference?

Regards,

Peter

arunpatidar
Community Advisor
Community Advisor
May 15, 2019

Addition to Peters response, no need to use @Component annotation with @Model

Arun Patidar
smacdonald2008
smacdonald2008Accepted solution
Level 10
May 15, 2019
smacdonald2008
Level 10
May 15, 2019

alao Do not use @compnent and @model like that. If you use @model, you do not use @ component,