Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

resourceresolver null java.lang.NullPointerException

Karl515
Level 5
Level 5

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)

1753127_pastedImage_8.png

1752970_pastedImage_3.png

my system-user

1753124_pastedImage_4.png

1753125_pastedImage_5.png

Bundle Name:

1753126_pastedImage_6.png

Why do I get?

1753128_pastedImage_9.png

1 Accepted Solution
smacdonald2008
Correct answer by
Level 10
Level 10
4 Replies
Peter_Puzanovs
Community Advisor
Community Advisor

Hey Karl,

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

Regards,

Peter

Arun_Patidar
Community Advisor
Community Advisor

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

smacdonald2008
Correct answer by
Level 10
Level 10
smacdonald2008
Level 10
Level 10

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