Query Builder createQuery code is not working in local Showing java.lang.NullPointerException? | Community
Skip to main content
Level 3
September 17, 2021
Solved

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

  • September 17, 2021
  • 3 replies
  • 3898 views

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.

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi @vijayalakshmi_s ,

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


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

3 replies

Asutosh_Jena_
Community Advisor
Community Advisor
September 17, 2021

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!

Level 3
September 17, 2021

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

Asutosh_Jena_
Community Advisor
Community Advisor
September 17, 2021

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!

Kiran_Vedantam
Community Advisor
Community Advisor
September 17, 2021

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.

 

Level 3
September 17, 2021

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

 

 

 

          

Vijayalakshmi_S
Level 10
September 17, 2021

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.

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.
Adobe Employee
September 20, 2021

Remove the '/' at the end of the property name.