4

I have a code like this in my project:

public class MyExpAdapter extends BaseExpandableListAdapter
{

    @Override
    public int getChildrenCount(final int groupPosition)
    {
        return this.getCountry(groupPosition) == null ? 0 :
                this.getCountry(groupPosition).getCityList() == null ? 0 :
                        this.getCountry(groupPosition).getCityList().size();
    }
.
.
.
}

Although I'm checking null probability, inspector is still highlighting last two lines as displayed in image below. Any idea? thanks.

enter image description here

Update

I even changed my method in a way to not use ternary operator however it still sucks :( enter image description here

Hesam
  • 52,260
  • 74
  • 224
  • 365

1 Answers1

5

Sorry, this should have been a comment on your question, since i don't have enough reputation points to add a comment i use this option. it is difficult to say something since we don't know the IDE you use.

But definitely this.getCountry(groupPosition) should be called once and result should be assigned to a local variable and then it should be used to avoid multiple unnecessary method calls.

please post the code of getCountry method as well because inspection might think that second time it will give a different value.

hunter
  • 3,963
  • 1
  • 16
  • 19
  • Thanks man, name of IDE has tagged to the question. I'll check your last line of answer and accept your answer if it is the reason. I guess you are right but let me double check. :thumbs_up – Hesam Aug 03 '15 at 05:00
  • I guess this is a nice answer. As an extra info, [this one](http://stackoverflow.com/a/17271106/3998458) say that Android Studio is too sensible with the NPE and he just ignore them (only if he is sure). And checking [this site](http://howtodoinjava.com/2013/04/05/how-to-effectively-handle-nullpointerexception-in-java/) they suggest that we should avoid chained methods calls (`object.method1().method2().etc()`), because they are not NPE friendly. – Alex S. Diaz Aug 03 '15 at 05:18