4

In my example below I want to avoid writing getView != null every time I want to use getView. To keep it cleaner I create a method hasView() which does the check for me. However I still get a warning. Is there any way around this?

import android.support.annotation.Nullable;

public void showView(){
    if(hasView()){
        getView().show(); // Shows warning Method invocation 'showLoading' may produce 'java.lang.NullPointerException'
    }
}


boolean hasView(){
    return getView() != null;
}

@Nullable
private View getView(){
    return view;
}

I am using Android Studio/IntelliJ. I know that I can use @SuppressWarnings I have seen this question but this makes the code uglier.

Ubaier Bhat
  • 854
  • 13
  • 33

2 Answers2

7

I want to avoid writing getView != null every time I want to use getView ?

You can use Null Object pattern to avoid checking for != null everywhere in the program, the code is shown below:

(1) Define an EmptyView Class

   public EmptyView {

     //Define a static emptyView, so that we can reuse the same object
     public static final EmptyView emptyView = new EmptyView();

     public show() {
         //does nothing
      }
    }

(2) Use the EmptyView in case of no view available:

    //other classes:
    private View getView(){
        if(viewAvailable) {
           return view;
        } else {
            return EmptyView.emptyView;
        }  
    }

    public void showView(){
        getView().show();
    }

You can look at Wiki here more info & Java example.

When you return null from various methods, they will potentially cause the NullPointerException in the programs and will become very hard to debug in larger projects.

So, it is a bad practice to return null from methods (& they should be avoided) and that is the reason Spring API generally return empty list/set objects (instead of null) while trying to retrieve data from DAO/Repository classes (like EmptyView object as explained above).

P.S.: This option works with and without Java8. If you are using Java8, then prefer to do with Optional as given in the answer from @janos

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Thanks. I am not using Java8 so I will have to use this pattern. Only annoying thing is that every time I add a method to the View I have to update it to EmptyView as well. – Ubaier Bhat Nov 12 '16 at 20:59
  • Can you tell me an example method like what you need to add ? – Vasu Nov 12 '16 at 21:02
  • 1
    Good suggestion. You might also avoid doing "new EmptyView() " if you define a static instance of EmptyView. Since it is "empty", it is immutable by definition and there's no side effect in reusing it. – Insac Nov 12 '16 at 21:08
  • @javaguy I meant that if I add have say getView().hide() or getView().foo() then I will have to add these to the EmptyView as well. – Ubaier Bhat Nov 12 '16 at 21:15
  • @Insac Thanks for your suggestion, added the static instance to reuse – Vasu Nov 12 '16 at 21:15
  • @UbaierBhat you are right, to avoid null checks at multiple places, we need to do this in only one single place – Vasu Nov 12 '16 at 21:17
4

You might be interested in using an Optional, added in API level 24 of Android, for example:

private Optional<View> getView() {
    return Optional.ofNullable(view);
}

public void showView() {
    getView().ifPresent(View::show);
}
janos
  • 120,954
  • 29
  • 226
  • 236