0
      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

amit
  • 23
  • 1
  • 5
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Sep 02 '19 at 10:35
  • Does the factory object `resolverFactory` exist? – Koenigsberg Sep 02 '19 at 10:39
  • post full code and full exception – awd Sep 03 '19 at 04:12
  • Are you trying to run this code on a publish instance, maybe with an anonymous user? BTW do NOT use the node API to manipulate repository data, use the resource API instead. – Oliver Gebert Sep 03 '19 at 07:57

5 Answers5

1

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

1

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

0

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;
Jithin Kumar
  • 49
  • 1
  • 9
  • Please try to inject resource resolver using the annotation stated above so that the lifecycle of the bean and retrieval of the instance from the Factory is directly managed by OSGI container – Jithin Kumar Sep 02 '19 at 14:12
0

It should be corrected as below.

resolver=resolverFactory.getServiceResourceResolver(param);
0

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.");
halfer
  • 19,824
  • 17
  • 99
  • 186
Himanshu Sharma
  • 133
  • 2
  • 8