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?
Solved! Go to Solution.
Views
Replies
Total Likes
See this where this use case works fine.
Views
Replies
Total Likes
Hey Karl,
Shouldn't you be using @OSGiService for your ResourceResolverFactory, instead of @Reference?
Regards,
Peter
Views
Replies
Total Likes
Addition to Peters response, no need to use @Component annotation with @Model
See this where this use case works fine.
Views
Replies
Total Likes
alao Do not use @compnent and @model like that. If you use @model, you do not use @ component,
Views
Replies
Total Likes
Views
Likes
Replies