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
BedrockMission!

Learn More

View all

Sign in to view all badges

How to combine two ValueMaps?

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

171 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

171 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

24-04-2021

trying to combine 2 valuemaps into 1.   putAll fails with null pointer, and it doesnt not seem possible to create an empty valueMap.  Any ideas for another option? I have tried posting the full info on why we are doing this, but the forum system always deletes it, so trying this short version.

 

		predicate.put("path", path);
		predicate.put("type", "cq:unstructured");
		predicate.put("group.1_property", "sling:resourceType");
		predicate.put("group.1_property.value", "mysite/components/gamemain");
		predicate.put("group.2_property", "sling:resourceType");
		predicate.put("group.2_property.value", "mysite/components/gameterms");
		predicate.put("group.p.or", "true");
		
		Query query = _builder.createQuery(PredicateGroup.create(predicate), session);
		
		// should only be 2 components!
		query.setStart(0);
		query.setHitsPerPage(20);
		
		SearchResult searchResult = query.getResult();
		
		ValueMap allProps = null;
		
		for(Hit hit : searchResult.getHits()) {
			ValueMap props = hit.getProperties();
			if (allProps == null) {
				allProps  = props;
			}  else {
				allProps.putAll(props);  // null pointer exception
			}
		}

 

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Give Back 50
MVP
Vijayalakshmi_S
MVP

Likes

436 likes

Total Posts

574 posts

Correct Reply

191 solutions
Top badges earned
Give Back 50
Give Back 5
Ignite 10
Ignite 5
Ignite 3
View profile

Avatar
Give Back 50
MVP
Vijayalakshmi_S
MVP

Likes

436 likes

Total Posts

574 posts

Correct Reply

191 solutions
Top badges earned
Give Back 50
Give Back 5
Ignite 10
Ignite 5
Ignite 3
View profile
Vijayalakshmi_S
MVP

26-04-2021

Hi @TB3dock,

Ways to instantiate an empty ValueMap is through its implementing Decorator classes. 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);

Example :

 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);
/* Within Query results iteration */
for (Hit hit : queryResults.getHits()) {
			try {
				String pagePath = hit.getPath();								
				vm.putAll(hit.getProperties());
....

 

Below API doc has the list of all known implementing classes of ValueMap, you can choose the one based on your need. (Ignore Deprecated ones from the list)

https://sling.apache.org/apidocs/sling8/org/apache/sling/api/resource/ValueMap.html

Answers (1)

Answers (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

271 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

271 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

26-04-2021

@TB3dock 

You will get NullPointerException if any one of variables is null. 

allProps.putAll(props);

allProps and props should not be null. Please check.