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
BedrockMission!

Learn More

View all

Sign in to view all badges

resourceresolver null java.lang.NullPointerException

Avatar

Avatar
Validate 25
Level 4
Karl515
Level 4

Likes

14 likes

Total Posts

157 posts

Correct Reply

11 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
Level 4
Karl515
Level 4

Likes

14 likes

Total Posts

157 posts

Correct Reply

11 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
Karl515
Level 4

14-05-2019

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

View Entire Topic

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,406 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,406 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

15-05-2019

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