Expand my Community achievements bar.

Facing issue while implementing Dynamic Dropdown

Avatar

Level 6

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]
14 Replies

Avatar

Community Advisor

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

Avatar

Community Advisor

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 

Avatar

Level 6

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());
                        }
                    }
                }

Avatar

Community Advisor

@Keerthi0555 

 

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


Aanchal Sikka

Avatar

Level 6

*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 com.adobe.granite.ui.components.ds.ValueMapResource.getChild(ValueMapResource.java:159) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
at com.adobe.granite.ui.components.rendercondition.RenderConditionHelper.getRenderCondition(RenderConditionHelper.java:105) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
at com.adobe.granite.ui.components.ComponentHelper.getRenderCondition(ComponentHelper.java:525) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
at org.apache.jsp.libs.granite.ui.components.coral.foundation.form.select.render_jsp.printOption(render_jsp.java:47)
at org.apache.jsp.libs.granite.ui.components.coral.foundation.form.select.render_jsp._jspService(render_jsp.java:293)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [org.apache.sling.scripting.jsp:2.3.6]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) [org.apache.felix.http.servlet-api:1.2.0]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:339) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:97) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:600) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) [org.apache.sling.scripting.core:2.3.2]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.3.2]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.3.2]
at com.adobe.granite.ui.components.impl.BaseComponentHelper.call(BaseComponentHelper.java:568) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
at com.adobe.granite.ui.components.ComponentHelper.call(ComponentHelper.java:758) [com.adobe.granite.ui.commons:5.10.15.CQ650-B0008]
at org.apache.jsp.libs.granite.ui.components.coral.foundation.form.field.field_jsp._jspService(field_jsp.java:312)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [org.apache.sling.scripting.jsp:2.3.6]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) [org.apache.felix.http.servlet-api:1.2.0]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:339) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:97) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:600) [org.apache.sling.scripting.jsp:2.3.6]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) [org.apache.sling.scripting.core:2.3.2]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.3.2]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.3.2]
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:579) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:88) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.wcm.core.impl.WCMDeveloperModeFilter.doFilter(WCMDeveloperModeFilter.java:119) [com.day.cq.wcm.cq-wcm-core:5.12.216]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:156) [com.day.cq.wcm.cq-wcm-core:5.12.216]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.dynamicinclude.IncludeTagFilter.doFilter(IncludeTagFilter.java:74) [org.apache.sling.dynamic-include:3.3.0]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.adobe.acs.commons.granite.ui.components.impl.include.IncludeDecoratorFilterImpl.doFilter(IncludeDecoratorFilterImpl.java:92) [com.adobe.acs.acs-aem-commons-bundle:5.0.12]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:165) [com.day.cq.wcm.cq-wcm-core:5.12.216]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.wcm.core.impl.page.PageLockFilter.doFilter(PageLockFilter.java:91) [com.day.cq.wcm.cq-wcm-core:5.12.216]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:94) [com.day.cq.cq-personalization:5.12.48]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at com.adobe.granite.csrf.impl.CSRFFilter.doFilter(CSRFFilter.java:217) [com.adobe.granite.csrf:1.0.20.CQ650-B0002]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:283) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.dispatchRequest(SlingRequestProcessorImpl.java:323) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.request.SlingRequestDispatcher.dispatch(SlingRequestDispatcher.java:211) [org.apache.sling.engine:2.7.10.B0004]
at org.apache.sling.engine.impl.request.SlingRequestDispatcher.include(SlingRequestDispatcher.java:104) [org.apache.sling.engine:2.7.10.B0004]
at com.day.cq.wcm.core.impl.WCMComponentFilter$ForwardRequestDispatcher.include(WCMComponentFilter.java:516) [com.day.cq.wcm.cq-wcm-core:5.12.216]
at org.apache.sling.scripting.jsp.taglib.IncludeTagHandler.dispatch(IncludeTagHandler.java:54) [org.apache.sling.scripting.jsp.taglib:2.4.0]
at org.apache.sling.scripting.jsp.taglib.AbstractDispatcherTagHandler.doEndTag(AbstractDispatcherTagHandler.java:128) [org.apache.sling.scripting.jsp.taglib:2.4.0]
at org.apache.jsp.libs.granite.ui.components.coral.foundation.form.fieldset.fieldset_jsp._jspService(fieldset_jsp.java:187)
at 



Avatar

Community Advisor

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

Avatar

Employee

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
 

Avatar

Level 6

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

 

Avatar

Employee

Can you please check and confirm if "ds" value is coming as not null, you can add a breakpoint to confirm that. Also, if you'll "step-into" the req.setAttribute(DataSource.class.getName(), ds) line it'll take you to that exact internal class where it is failing. You can check which particular variable is getting initialized as null and back-track from there to fix it.

Also, based upon the error logs, there is a chance error might be getting generated from cq:dialog file. Please check that as well. 

 

Avatar

Level 6

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

Avatar

Level 6

I think it's from the component but not sure how to fix this.

Avatar

Administrator

@Keerthi0555 , can you please share how did you fix this? This would help in posterity. 



Kautuk Sahni