Warning in Equals supposedly null

3

I have the following Warning:

  

Method invocation 'equals' may produce   'java.lang.NullPointerException'

My line of code is as follows:

    if(menu.getItem(2).getIcon().getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning).getConstantState()))

I have never generated an error, but I do not understand the warning at all, what I am doing is comparing the icon of my top menu with an image that I have in the mipmap directory.

EDIT:

The same thing happens to me in the other if with menu.getItem(0) and menu.getItem(1)

EDITO2:

This happens in my method "filterChecks" where I keep everything selected in "Sort by", "Filter by" or "Search" (this depends on the RadioButtom, CheckButtom and an EditText that has a menu), and I pass it by an adapter that reorders and / or hides elements of an ExpandableListView

EDITO3:

I need to do this equality outside the method onOptionsItemSelected (MenuItem item) {...} Do not insist. This is my onOptionsItemSelected

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        this.item = item;
        switch(item.getItemId()){
            case R.id.ordenar_item:
                //Desaparecen todos los demás
                if(filtrosLl.getVisibility()==View.VISIBLE)
                    filtrosLl.setVisibility(View.GONE);
                if(busquedaMet.getVisibility()==View.VISIBLE)
                    busquedaMet.setVisibility(View.GONE);

                //Aparece/desaparece Ordenar Por
                if(prioridadesLl.getVisibility()==View.GONE) {
                    prioridadesLl.setVisibility(View.VISIBLE);
                }else{
                    prioridadesLl.setVisibility(View.GONE);
                }
                return true;
            case R.id.filtrar_item:
                //Desaparecen todos los demás
                if(prioridadesLl.getVisibility()==View.VISIBLE)
                    prioridadesLl.setVisibility(View.GONE);
                if(busquedaMet.getVisibility()==View.VISIBLE)
                    busquedaMet.setVisibility(View.GONE);

                //Aparece/desaparece Filtrar Por
                if(filtrosLl.getVisibility()==View.GONE) {
                    filtrosLl.setVisibility(View.VISIBLE);
                    //mensajeFiltrosTv.setVisibility(View.GONE);
                }else {
                    filtrosLl.setVisibility(View.GONE);
                    /*if(!TextUtils.isEmpty(mensajeFiltrosTv.getText()))
                        mensajeFiltrosTv.setVisibility(View.VISIBLE);*/
                }
                return true;
            case R.id.buscar_item:
                //Desaparecen todos los demás
                if(prioridadesLl.getVisibility()==View.VISIBLE)
                    prioridadesLl.setVisibility(View.GONE);
                if(filtrosLl.getVisibility()==View.VISIBLE)
                    filtrosLl.setVisibility(View.GONE);

                //Aparece/desaparece Buscar Por
                if(busquedaMet.getVisibility()==View.GONE) {
                    busquedaMet.setVisibility(View.VISIBLE);
                }else{
                    busquedaMet.setVisibility(View.GONE);
                }
                return true;
            case android.R.id.home:
                fragmentActivity.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
asked by Maguz 15.12.2016 в 21:36
source

5 answers

1

The warning indicates that it is not a good programming practice to assign the values of the ID in a constant way:

 menu.getItem(2);

because the ID could exist or not. It is advisable to obtain the ID in a more secure way (that does not cause an exception in runtime):

menu.findItem(R.id.miMenu);

ó:

item.getItemId();

An example of the second case:

my_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/hello"
          android:icon="@drawable/ic_hello"
          android:title="@string/shello" />
    <item android:id="@+id/bye"
          android:icon="@drawable/ic_bye"
          android:title="@string/sbye" />
</menu>

In Activity

Inflate the Menu Resource:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

Assign an action for each menu item:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.hello:
        sayHello();
        return true;
    case R.id.bye:
        sayBye();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
    
answered by 22.12.2016 в 18:19
0

You should not get the menu items by means of the index, since you can use an index that does not correspond to an element and you can mark as it mentions the message:

  

Method invocation 'equals' may produce   'java.lang.NullPointerException'

It is advisable to obtain the items according to your id, example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu1:

      // if(menu.getItem(2).getIcon().getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning).getConstantState()))

     if (item.getIcon().getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning).getConstantState()))
      ...
      ...



        return true;
    case R.id.menu2:

     if (item.getIcon().getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.imagen2).getConstantState()))
      ...
      ...



        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
  }   

}
    
answered by 16.12.2016 в 00:14
0

As you well say is a warning therefore it is a warning. The warning refers to that for your menu, specifically for the items it can be defined:

  • item 1
  • item 2
  • item 3

But it warns you because CAN exist the possibility that for the getItem(i) method there is no reference for this index and this could cause a exception of null. You can put getItem(1000) and it will give you the warning, however at run time your application will generate said exception

EDITING

According to my perspective, to make disappear that warning by code I see it very very difficult, I do not understand the intention of hiding it, when it does not affect anything in only a warning , however if you do not want see it you can go:

Windows -> Preferences -> General -> Editors -> Text Editors -> Annotations 

You are looking for Warning (last option) and you check out the option Vertical ruler

    
answered by 15.12.2016 в 21:43
0

What I understand is that this part:

ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning)

It may turn out that it returns a value null , so it is possible that when wanting to obtain the following: .getConstantState() , I throw the error of NullPointeException .

You should first make sure that the first part does not return null:

if(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning) != null && menu.getItem(2).getIcon().getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning).getConstantState()))
    
answered by 23.12.2016 в 21:46
0

The warning appears because any of the elements of the string menu.getItem(2).getIcon().getConstantState() is annotated as @Nullable .

An annotation @Nullable means that a legitimate return value of the method could be null. Therefore you should check this condition before calling the next method in the chain. For example if getIcon() is the method annotated with @Nullable you can modify the conditional as follows:

Drawable icon = menu.getItem(2).getIcon();
if (icon != null && icon.getConstantState().equals(ContextCompat.getDrawable(fragmentActivity, R.mipmap.buscar_warning).getConstantState()))

It is recommended that you heed this warning. Since at some point could be the case and the application would be closed without any explanation. That can damage the image of the app

    
answered by 24.07.2017 в 17:01