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

Query Builder createQuery code is not working in local Showing java.lang.NullPointerException?

Avatar

Level 3

java.lang.NullPointerException------------------------

Query Builder createQuery code is not working in local When Im using query debugger for Testing of same predicated group objects it showing result but not with JAVA

 

Query query = queryBuilder.createQuery(PredicateGroup.create(searchMap), session);

 

This line not executing when im running the code.

 

Captuertertetre.PNG

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @keshav_goud,

With Service Implementation (of QueryBuilder) bound, it shouldn't be null. Try restarting the QueryBuilderImpl OSGi component and/or its bundle - com.day.cq.cq-search.

 

Can you try to change the way we reference the QueryBuilder. If you are annotating it to an instance variable, try it with method or vice versa (With impl class bound per the screenshot you have shared, this shouldn't really make a difference. Just a try though)

 

Field/Instance variable annotation:

@Reference
private QueryBuilder queryBuilder;

Method annotation:

private QueryBuilder queryBuilder;
    
@Reference
protected void bindQueryBuilder(QueryBuilder queryBuilder){
        this.queryBuilder = queryBuilder;
}

Can you share the complete servlet code if possible

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @keshav_goud 

 

Can you please put your code here? Which line is throwing the error? I hope you are using system user to get the session.

 

Thanks!

Avatar

Level 3

Hi @Asutosh_Jena_ ,

 

Query query = queryBuilder.createQuery(PredicateGroup.create(searchMap), session);

 

this line giving null in try catch mode and session is from request.getResourceResolver(); it is a servlet im using

Avatar

Community Advisor

Hi @keshav_goud 

 

Please use the below query.

Map<String, String> predicatesMap = new HashMap<>();
//Generate the query here. Adding a sample below for now.
predicatesMap.put(PREDICATE_FULLTEXT, Text.unescape(keyWord));
predicatesMap.put(PREDICATE_TYPE, NameConstants.NT_PAGE);
predicatesMap.put(PREDICATE_PATH, searchRootPath);
predicatesMap.put(PREDICATE_PROPERTY_OPERATION, PROPERTY_OPERATION_EXISTS);
predicatesMap.put(PREDICATE_PROPERTY_VALUE, String.valueOf(Boolean.FALSE));
predicatesMap.put(PREDICATE_LIMIT, "-1");

final PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
final Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
query.setExcerpt(Boolean.TRUE);
final SearchResult result = query.getResult();

 Hope this helps!

 

Thanks!

Avatar

Level 3

hi @Asutosh_Jena_ ,

           1.Query builder im calling in servlet by using @Reference

           2.Same its again null showing here im using two properties comparing in a single resource both. can you give me that query in this way that appreciated. The  query be likeCaptuertertetre.PNG

 

Avatar

Community Advisor

Hi @keshav_goud,

 

  1. First try to run the query in http://localhost:4502/libs/cq/search/content/querydebug.html
    1. If this is returning the results, then you can expect the issue in the code
  2. Secoundly, check how the session is being created [the resourceresolver]
    1. the user should have the proper access to get the results on the content path 
  3. Further to debug, go to error.log and check the error that the java code is throwing. it would specifically say which line of code is throwing the null pointer 
  4. You can also debug the code using your IDE and check where the issue is.

Hope this helps.

 

Thanks,

Kiran Vedantam.

 

Avatar

Level 3

HI @Kiran_Vedantam ,

 

          1. Query debugger results showing.

          2. Currently im implementing in Servlet .Session Im getting from Resolver and it is from request                    not with the user mapping.

          3. In JAVA code while im trying to print the class of query builder its showing NULL in logs

               queryBuilder.createQuery(PredicateGroup.create(searchMap), session) throwing null.

 

 

Thanks,

Keshav

 

 

 

          

Avatar

Community Advisor

Hi @keshav_goud,

Can you cross check your servlet class in OSGi components console(http://localhost:4502/system/console/components) if it is active and if queryBuilder reference is satisfied/Bound to respective Service implementation.

Vijayalakshmi_S_0-1631899326808.png

If it is not, cross check

  • the import statements of @Reference(org.osgi.service.component.annotations.Reference) and QueryBuilder(com.day.cq.search.QueryBuilder) from code standpoint. 
  • If the respective service implementation is active from instance standpoint.

Avatar

Level 3

Hi @Vijayalakshmi_S ,

                  Its in Satisfied Status and active also ,any other solutions please suggest me.

Captsdfsfsure.PNG

Avatar

Correct answer by
Community Advisor

Hi @keshav_goud,

With Service Implementation (of QueryBuilder) bound, it shouldn't be null. Try restarting the QueryBuilderImpl OSGi component and/or its bundle - com.day.cq.cq-search.

 

Can you try to change the way we reference the QueryBuilder. If you are annotating it to an instance variable, try it with method or vice versa (With impl class bound per the screenshot you have shared, this shouldn't really make a difference. Just a try though)

 

Field/Instance variable annotation:

@Reference
private QueryBuilder queryBuilder;

Method annotation:

private QueryBuilder queryBuilder;
    
@Reference
protected void bindQueryBuilder(QueryBuilder queryBuilder){
        this.queryBuilder = queryBuilder;
}

Can you share the complete servlet code if possible

Avatar

Level 1

you are amazing!! I had been looking for ever and didn't realize I had the wrong import statement... thank you!