amit
amit

Reputation: 23

I am trying to get a reference to the ResourceResolver from the ResourceResolverFactory but got Null pointer Exception

      Map<String, Object> param = new HashMap<String, Object>();
      param.put(ResourceResolverFactory.SUBSERVICE,"datawrite");
      log.info("param created."); 
      ResourceResolver resolver = null;  
    try {
         resolver=resolverFactory.getResourceResolver(param);
         log.info("resolveer created.");   
      Session session = resolver.adaptTo(Session.class);
      log.info("Session created.");
        // Create a node that represents the root node
        Node root = session.getRootNode();
        // Get the content node in the JCR
        Node content = root.getNode("/content");        
        Node customerRoot = null;
        int custRec = doesCustExist(content);
                log.info("does Customer Exist : "+ custRec);
        // -1 means that content/customer does not exist
        if (custRec == -1) {
            // content/customer does not exist -- create it
            customerRoot = content.addNode("customer");
        } else {
            // content/customer does exist -- retrieve it
            customerRoot = content.getNode("customer");
        }

        int custId = custRec + 1; // assign a new id to the customer node
    // Store content from the client JSP in the JCR
        Node custNode = customerRoot.addNode("customer" + firstName + lastName + phone+desc);
    // make sure name of node is unique
        custNode.setProperty("id", custId);
        custNode.setProperty("firstName", firstName);
        custNode.setProperty("lastName", lastName);
        custNode.setProperty("phone", phone);
        custNode.setProperty("desc", desc);
        // Save the session changes and log out
        session.save();
        session.logout();
        return custId;
    }
catch (Exception e) {
        log.error("RepositoryException: " + e);
    }

i got this error:

ERROR [0:0:0:0:0:0:0:1 [1567433510240] GET /bin/abc HTTP/1.1] aem.community.mf.core.servlets.SaveJcrData RepositoryException: java.lang.NullPointerException

Upvotes: 0

Views: 3697

Answers (5)

Himanshu Sharma
Himanshu Sharma

Reputation: 133

You can solve this problem in two ways:

  1. Try using ResourceResolverFactory in servlet itself, means you should define it in servlet itself and use it there for getting resources and use

 resolver=req.getResourceResolver();

instead of

resolver=resolverFactory.getResourceResolver(param);

  1. You should ensure that your class should be annotated as @Component and then try to get resources in that class as follows:

@Component
public class SaveJcrData implements SaveService {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    private SlingRepository repository;

    @Reference
    private ResourceResolverFactory resolverFactory;

    private Session session;

    public void bindRepository(SlingRepository repository) {
        this.repository = repository;
    }

//Stores customer data in the Adobe CQ JCR
    public int injestData(String firstName, String lastName, String phone, String desc) throws Exception {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
        ResourceResolver resolver = null;
        try {

            // Invoke the adaptTo method to create a Session used to create a QueryManager
            log.trace("In try.");
            resolver = resolverFactory.getServiceResourceResolver(param);                                                                           
                                                                            
            log.trace("resolveer created.");
            Session session = resolver.adaptTo(Session.class);
            log.info("Session created.");

Upvotes: 0

Sudheer Donaboina
Sudheer Donaboina

Reputation: 416

Could you please share the complete stacktrace. Try to print stack trace (instead of just the message) all the time to easily identify the issue at the correct line of code.

When we are trying to use a different user for the operation we have to use the getServiceResourceResolver API to get a user based resource resolver.

Here is a sample servlet for modifying content/view in a servlet

https://github.com/sudheerdvn/aem-flash/blob/develop/core/src/main/java/com/flash/aem/core/servlets/ModifyContentServlet.java

You can view the result by hitting the URL directly as below (Added get method for the same for view purpose)

http://localhost:1502/bin/modifyContent.json

Upvotes: 1

Net Solutions
Net Solutions

Reputation: 145

If you are trying to fetch the resource resolver in Sling Model, use the following annotation.

@Inject
  private ResourceResolver resourceResolver;

Also make sure your class is getting called from AEM. To explain more as per my previouis encounters, here are some examples:

Custom Worklow Process - Using @Reference annotation you will be able to get the resource resolver because the process is called directly from AEM when the workflow reaches the step

Class A which is called from another class B- In this case the resource resolver will be null in Class A, as this class is not getting called from AEM

Upvotes: 1

Isuri Subasinghe
Isuri Subasinghe

Reputation: 147

It should be corrected as below.

resolver=resolverFactory.getServiceResourceResolver(param);

Upvotes: 0

Jithin Kumar
Jithin Kumar

Reputation: 49

It looks like you have not properly instantiated the Resource Resolver Factory.

You could Inject the resource resolver using SlingObject annotation from the context instead of retrieving it using the Factory.

eg

@SlingObject
private ResourceResolver resourceResolver;

Upvotes: 0

Related Questions