Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Current Node is NULL in Components

Avatar

Level 2

Hi,

Am newbie to AEM. Am creating pages with component included like <cq:include path="" resourceType="/customComponent"/>. And i created dialog for getting text field and path field value. 

And now am trying to access my current node in customComponent.jsp using both way  PropertyIterator it = currentNode.getProperties() or resourceResolver.adaptTo(Node.class) but the currentNode throws Null Pointer Exception . Now i need to get all Properties from multi field and path field components. How can i achieve this. Please give me some solution and thanks in advance.

P.S: Am added global.jsp too.

Am got stuck up with this issue.

Thanks,

Mohamedh Nishadh

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello,

There are bunch of implicit objects available in your servlet (jsp) when you use global.jsp or explicitly call <cq:defineObjects/> and one of them is "properties" which is a direct map of properties from your current node so you need not require to do currentNode.getProperties() just use properties also refer http://docs.adobe.com/docs/en/cq/5-6-1/developing/components.html

now to access a single value property from node use -String val= properties.get(propertyName, String.class);  // will return null if value not available and if you want to prevent from null then pass an empty string instead String.class

to access multivalue property (like multifield) from node then use - String []val=(String[])properties.get(propertyName,String[].class);

 

Thanks,

Pawan

View solution in original post

7 Replies

Avatar

Level 4

Hi Nishadh,

Try to give entire path in resource type

Ex : 

<cq:include path="mynode" resourceType="myapp/components/somecomponent" />

Avatar

Level 10

This looks like the pages are not properly setup - that is why you are getting nulls on nodes.  

As a new user - i highly recommend that you go through this content to learn how to setup an AEM site and pages:

http://docs.adobe.com/docs/en/aem/6-0/develop/the-basics/website.html

If the pages are setup properly - then it works.

Look at this community article that uses the SLign API and get page node properties:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

You can even pass nodes from front end component to back end OSGi services and the nodes are not null:

https://helpx.adobe.com/experience-manager/using/passing_nodes.html

Avatar

Level 4

Try to get the property like this

Title : <%=currentNode.getProperty("jcr:title").getString()%>

Avatar

Level 10

As a new user - i recommend that you look at this artilce:

http://docs.adobe.com/docs/en/aem/6-0/develop/the-basics/website.html

Once you properly setup an AEM site and nodes - you will not get NULLs on nodes. 

See this artilce on using SLING API to read nodes:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

Avatar

Correct answer by
Community Advisor

Hello,

There are bunch of implicit objects available in your servlet (jsp) when you use global.jsp or explicitly call <cq:defineObjects/> and one of them is "properties" which is a direct map of properties from your current node so you need not require to do currentNode.getProperties() just use properties also refer http://docs.adobe.com/docs/en/cq/5-6-1/developing/components.html

now to access a single value property from node use -String val= properties.get(propertyName, String.class);  // will return null if value not available and if you want to prevent from null then pass an empty string instead String.class

to access multivalue property (like multifield) from node then use - String []val=(String[])properties.get(propertyName,String[].class);

 

Thanks,

Pawan

Avatar

Level 4

I used String []val=(String[])properties.get(propertyName,String[].class) to retreive value, how do I add value to a multifield?

Avatar

Community Advisor

You have to create an array of values:

ValueFactory valueFactory = session.getValueFactory(); 
Node node = session.getNode("/content/path/to/my/node");
Value[] values = new Value[3];
values[0] = valueFactory.createValue("First value");
values[1]  = valueFactory.createValue("Second value");
values[2] = valueFactory.createValue("Third value");

node.setProperty("propertyName", values);

alternatively, you may use a String array:

node.setProperty("propertyName", new String[] {"First value", "Second value", "Third value"}); 

Node :

since node is global , you need to adapt from resource object or from SUBSERVICE session.