2

I'm trying get the id of several fragments that may or may not have been initialized yet using

int i = myFragment.getId();, int j = mySecondFragment.getId();.

My question: Is there a cleaner/better way of getting the id of each valid fragment other than

if (myFragment != null) {int i = myFragment.getId();}?

Igor
  • 33,276
  • 14
  • 79
  • 112
Lee
  • 1,028
  • 2
  • 11
  • 18
  • 3
    Check out this question: http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java?rq=1 – default locale Mar 27 '13 at 17:59
  • Can you show the code and what you are trying to use this for? I can't tell what he best way is with that much information. – Rarw Mar 27 '13 at 18:01
  • Depending on what you do with the id you could replace the null fragments with a dummy implementation (a singleton maybe) that always returns an invalid id or one that does not change the result. Let's say you want to add all the IDs. Then you could have DummyFragment extends Fragment and getID() { return 0; }. This way it doesn't matter how many 0s you have the result is the same. – Vlad Topala Mar 27 '13 at 18:11

2 Answers2

2

You could use Google Guava for that.

Of course similar functionality could be implemented by you, but Guava gives some amazing examples.

For instance, imagine that an argument could come as null, let's call it Optional the idea is that you fail fast if it is, just don't accept nulls. Or even, define a default value for it.

public void method(Integer arg){
    Optional<Integer> possible = Optional.fromNullable(arg);
    Integer myNum = possible.get(); // Throws exception if arg is null
    Integer myNum2 = possible.or(0); // returns 0 if arg is null
}

The idea is that you don't let nulls get passed a certain level of your application, the ideal would for it to never get past the view (or the persistence layer on the way back from the DB).

If this idea is implemented all the way through. In the core of your business layer, you wouldn't have to worry about nulls.

There are many other things you can do with Guava, if you decide to follow my suggestion, be sure to make the most of it :)

Rodrigo Sasaki
  • 7,048
  • 4
  • 34
  • 49
  • This is probably what I'm looking for but having a bit of trouble translating it to the issue at hand. So `myFragment` is the possible `null` value here but I need the id from it or 0. So I'm assuming `Optional opFragment = Optional.fromNullable(myFragment);` will prevent the nullPointerException but how do I call `.get().getId();` without throwing the same exception? – Lee Mar 27 '13 at 20:35
  • You don't use `get` in that case, `get` will either return the correct value or fail fast if null, in that case you need to use `opFragment.or(0)` – Rodrigo Sasaki Mar 28 '13 at 11:07
0

If it's something you do a lot then you could make a function that does the checking for you. So instead of typing

if (myFragment != null) {int i = myFragment.getId();}

all the time you could do something like

public ID getFragmentId(Fragment myFragment){
    if (myFragment != null) {
        return myFragment.getId();
    } else {
        return someDefaultValue;
    }
}

And then just call

i = getFragmentID(myFragment);

Avoids a lot of repetetive typing and will make your code a bit clearer.

Jias
  • 164
  • 12