0

I have below code snippet but it shows warnings Method invocation 'getSearchableInfo' may produce 'NullPointerException'

ComponentName s = getComponentName();
if (s ==null)
    return;
@NonNull ComponentName componentName = getComponentName();
SearchableInfo searchableInfo = searchManager
   .getSearchableInfo(componentName); //<--  Warning Line

I could find samples for Kotlin but I don't know how to get rid of this warning in Java?

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
VSB
  • 9,825
  • 16
  • 72
  • 145

1 Answers1

0

When you have a NullPointerException calling a method with this messages:

java.lang.NullPointerException: Attempt to invoke virtual method getSearchableInfo

Generally you are gettin this error when the class that call the method (.getSearchableInfo()) was not initialized correctly.

Be sure to initialize SearchManager :

  //Initialize SearchManager
  SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  ...
  ...
  SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
Jorgesys
  • 124,308
  • 23
  • 334
  • 268