问题
public List<DHProductLookupModel> findProductbyCCsapProductId(final String code)
{
final String queryString = "SELECT {p:" + DHProductLookupModel.SAPPRODUCTID + "}" + "FROM{" + DHProductLookupModel._TYPECODE
+ " AS p}" + "WHERE" + "{p:" + DHProductLookupModel.SAPPRODUCTID + "}=?code ";
final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString);
query.addQueryParameter("code", code);
return flexibleSearchService.<DHProductLookupModel> search(query).getResult();
}
search(query) throws Null Pointer Exception, how to handle this?
Output:
Caused by: java.lang.NullPointerException
at de.hybris.platform.servicelayer.search.impl.DefaultFlexibleSearchService.getJaloResult(DefaultFlexibleSearchService.java:396) ~[coreserver.jar:?]
at de.hybris.platform.servicelayer.search.impl.DefaultFlexibleSearchService.search(DefaultFlexibleSearchService.java:168) ~[coreserver.jar:?]
at com.cancom.core.productlookup.dao.impl.CCProductLookupDaoImpl.findProductbyCCmanufacturerProductId(CCProductLookupDaoImpl.java:39) ~[classes/:?]
at com.cancom.core.productlookup.service.impl.CCProductLookupServiceImpl.getProductforCCmanufacturerProductId(CCProductLookupServiceImpl.java:37) ~[classes/:?]
Thanks!
回答1:
Change your flexible search query from
final String queryString = "SELECT {p:" + DHProductLookupModel.SAPPRODUCTID + "}" + "FROM{" + DHProductLookupModel._TYPECODE
+ " AS p}" + "WHERE" + "{p:" + DHProductLookupModel.SAPPRODUCTID + "}=?code ";
to
final String queryString = "SELECT {p:" + DHProductLookupModel.PK + "}" + "FROM{" + DHProductLookupModel._TYPECODE
+ " AS p}" + "WHERE" + "{p:" + DHProductLookupModel.SAPPRODUCTID + "}=?code ";
You should send DHProductLookupModel.PK
in search result.
回答2:
In your case, you can use getModelsByExample of flexibleSearchService instead of writing the query.
Your method will be like
public List<DHProductLookupModel> findProductbyCCsapProductId(final String code)
{
DHProductLookupModel dhProductLookupModel = new DHProductLookupModel();
dhProductLookupModel.setSapProductID(code);
return getFlexibleSearchService().getModelsByExample(dhProductLookupModel);
}
find the example here
回答3:
Thanks everyone! I found the Problem. It was in my spring.I just had to put :
<context:component-scan base-package="myPackage"/>
I changed also the query with DHProductLookupModel.PK for safety. Now it´s work!
来源:https://stackoverflow.com/questions/49923311/searchquery-throws-nullpointerexception