Facing issue while implementing Dynamic Dropdown | Community
Skip to main content
Keerthi0555
Level 5
July 5, 2023

Facing issue while implementing Dynamic Dropdown

  • July 5, 2023
  • 4 replies
  • 2590 views

I'm getting null pointer exception for the belwo code:

ResourceResolver resourceResolver;
    Resource pathResource;
    ValueMap valueMap;
    List<Resource> resourceList = new ArrayList<Resource>();
    public void doGet(SlingHttpServletRequest req, SlingHttpServletResponse res){
        pathResource = req.getResource();
        log.error("---42--servlet");
        String path = pathResource.getChild("datasource").getValueMap().get("tagsPath").toString();
        Resource tagsPath = req.getResourceResolver().getResource(path);
        for(Resource resource : tagsPath.getChildren()){
            valueMap = new ValueMapDecorator(new HashMap<>());
            Tag tag = resource.adaptTo(Tag.class);
            String tagFullName = tag.getTagID();
            String tagName = tagFullName.substring(tagFullName.lastIndexOf("/")+1);
            String tagTitle = tag.getTitle();
            log.error("tagName:{}", tagName);
            log.error("tagTitle:{}", tagTitle);
            valueMap.put("value", tagName);
            valueMap.put("text", tagTitle);
            try{
                 resourceList.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", valueMap));
            }
            catch(Exception ex){
                log.error("--exception--:{}", ex.getMessage());
            }
      }
        DataSource ds =  new SimpleDataSource(resourceList.iterator());
        req.setAttribute(DataSource.class.getName(), ds);
   
    }
=========================================
Exception:
java.lang.NullPointerException: null
at org.apache.sling.api.resource.AbstractResource.getChild(AbstractResource.java:78) [org.apache.sling.api:2.22.0]
at com.adobe.granite.ui.components.ds.ValueMapResource.getChild(ValueMapResource.java:159) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
July 5, 2023

Can you post the full log? It should indicate where the NULL pointer is being initiated. But most likely this is the line causing the issue:
Can you check if you have a node called "datasource" under the "pathResource" node?

String path = pathResource.getChild("datasource").getValueMap().get("tagsPath").toString();

 

Esteban Bustamante
DPrakashRaj
Community Advisor
Community Advisor
July 6, 2023

It seems issue might be located at this location

pathResource.getChild("datasource").getValueMap().get("tagsPath").toString(); You can check on code if any of the above is bill also please check the log correctly. Whenever there is an exception on the log trace it alleys show your java class line number in the log that will help you in isolate the line number of your code which is having the issue. Also, in your code you always need to check the null check for the resource/object that you create or instantiate 

Keerthi0555
Level 5
July 6, 2023

Hi,

I added null checks and in the logs I checked but I couldn't find the actual error in my servlet.

 if (pathResource.getChild("datasource") != null) {
            if (pathResource.getChild("datasource").getValueMap().get("tagsPath") != null)
                path = pathResource.getChild("datasource").getValueMap().get("tagsPath").toString();
            if (StringUtils.isNotBlank(path)) {
                Resource tagsPath = req.getResourceResolver().getResource(path);
                if (tagsPath.hasChildren()) {
                    for (Resource resource : tagsPath.getChildren()) {
                        valueMap = new ValueMapDecorator(new HashMap<>());
                        Tag tag = resource.adaptTo(Tag.class);
                        String tagFullName = tag.getTagID();
                        String tagName = tagFullName.substring(tagFullName.lastIndexOf("/") + 1);
                        String tagTitle = tag.getTitle();
                        log.error("tagName:{}", tagName);
                        log.error("tagTitle:{}", tagTitle);
                        valueMap.put("value", tagName);
                        valueMap.put("text", tagTitle);
                        try {
                            resourceList.add(new ValueMapResource(resourceResolver, new ResourceMetadata(),
                                    "nt:unstructured", valueMap));
                        } catch (Exception ex) {
                            log.error("--exception--:{}", ex.getMessage());
                        }
                    }
                }
aanchal-sikka
Community Advisor
Community Advisor
July 6, 2023

@keerthi0555 

 

requesting you to please share complete Log trace of the error. 

Aanchal Sikka
aanchal-sikka
Community Advisor
Community Advisor
July 6, 2023

hello @keerthi0555 

 

There are multiple improvements that can be done to the code. Specifically for NPE. Example, if conditions for 

  • pathResource.getChild("datasource") !=null
  • pathResource.getChild("datasource").getValueMap().get("tagsPath")!=null
  • tagsPath.hasChildren() should be checked before tagsPath.getChildren()

 

Maybe use SonarQube or plugins for IDE. They will help you immensely 

 

Aanchal Sikka
ayushmishra07
Adobe Employee
Adobe Employee
July 6, 2023

Hi @keerthi0555 , 

As a best practice it is advised that you always add a null and empty check when trying to read a property or initializing a dynamic object. Examples based on your code base can be : 

  •  req.getResource();
  • pathResource.getChild("datasource");
  •  pathResource.getChild("datasource").getValueMap()
  • pathResource.getChild("datasource").getValueMap().get("tagsPath")
  • req.getResourceResolver().getResource(path);
  • resource.adaptTo(Tag.class);

While handling the null and undefined checks would ensure you don't run into exceptions crashing the Sling Model, you might still not get the data as desired because there might be some logical error because of which you are running into that null exception.

Adding logging statements helps in debugging the code to identify the error but it'll be better if you attach a debugger to your AEM instance. Having a debugger attached you can set the breakpoints in to the code base and identify exactly where the error is happening. You can also dive into the internal functions to pinpoint the exact cause of the error. 

Setting up a debugger is a very straight-forward process, Please refer to the following links for setting up a debugger :
https://experienceleague.adobe.com/docs/experience-cloud-kcs/kbarticles/KA-17459.html?lang=en  ( For Setting Up Debugger in Eclipse )
https://aemgeeks.com/aem-debugging/how-to-start-aem-in-debug-mode/ ( For Setting Up Debugger in IntelliJ and Eclipse ) 
https://jimfrenette.com/2019/12/aem-app-debug-visual-studio-code/ ( For Setting Up Debugger in VSCode ) 

Thanks,
Ayush
 

Keerthi0555
Level 5
July 6, 2023

Hi Ayush,

Thank you for u r reply,

I set debugger and it's executing well till req.setAttribute(DataSource.class.getName(), ds); this line after this line it's going to some internal classes like ComponentHelper and getting /mnt/override/apps/mysite/components/componentfields/_cq_dialog.html/content/mysite/us/en/jcr:content/root/container/container/componentfields HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException this error

 
ayushmishra07
Adobe Employee
Adobe Employee
July 6, 2023

ds is not null.

Exceptions is *ERROR* [[0:0:0:0:0:0:0:1] [1688636235358] GET /mnt/override/apps/mysite/components/componentfields/_cq_dialog.html/content/mysite/us/en/jcr:content/root/container/container/componentfields HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
java.lang.NullPointerException: null
at org.apache.sling.api.resource.AbstractResource.getChild(AbstractResource.java:78) [org.apache.sling.api:2.22.0]
at


Can you please confirm on other points as well.