• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Type Mismatch

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I was creating a CronJob in Hybris and was facing this Type Mismatch compiler error on line number 44. Tried different things but unable to resolve it.
The error says : " Type mismatch. Cannot convert from SearchResult<Object> to SearchResult<HairProductsModel> "







 
Saloon Keeper
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
flexibleSearchService.search apparently does not return the type that you are assigning it to. Try to assign its result to a variable of type "SearchResult <Object>" instead.
 
Saloon Keeper
Posts: 9717
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could be that this is one of those cases where a method returns an "Object" because it could really return any of a number of types depending on the query. If you know the type that you're expecting you can cast it.
 
Master Rancher
Posts: 4322
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a fellow Hybris developer, I'm not sure what's going on here, because FlexibleSearchService.search() should support this.  Here's the method declaration (from the interface):

So yes, T is a type variable declared in the method, not elsewhere, so basically the compiler should be free to infer any type you declare here.  You can say

and the compiler will be happy with that (assuming necessary imports) even if the actual return type is not a SearchResult<Foo> but a SearchResult<Bar> - that will be discovered only at runtime, not compilation time.  And the part that errors out will not be this line, but the code that implements it, probably DefaultFlexibleSearchService.convert(SearchResult) and convertRow(Object), which insert casts to T.

At least, that's the way it should work.  Is it possible there was another version of this code recently, that used a SearchResult<Object> instead?  Is it possible there's still an old class file lying around somewhere?  I would do an ant clean all to make sure it's all built fresh.  Also, are you using an IDE?  IntelliJ with a suitable plugin like Integration for SAP Commerce?  That may help if you're not getting a clear view of the classes and methods you're working with.  If problems persist, what version of Hybris are you using?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:



I can't find any API anywhere, but what is the definition for the method the OP is using?
The one that takes a FlexibleSearchQuery parameter rather than a String?
 
Sheriff
Posts: 22700
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That also uses a method generic type. From https://help.sap.com/doc/a4265d5ea8314eb2929e6cf6fb8e35a5/1811/en-US/de/hybris/platform/servicelayer/search/FlexibleSearchService.html:
  • <T> SearchResult<T> search(FlexibleSearchQuery searchQuery)
  • <T> SearchResult<T> search(java.lang.String query)
  •  
    Saloon Keeper
    Posts: 14491
    325
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What Java compiler version are you using?

    Carey Brown wrote:If you know the type that you're expecting you can cast it.


    Even if type inference can not be used, it's better to explicitly pass the generic type argument than to cast the result:
     
    World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic