-2

I'm aware that getFlightKey is returning null during the initialising itself. It should be null as I'm using my code to build apks for two types of Users. For one user, "flight Key" is used and for the other, it is not used.

else {
            tabletMetadata = new TabletMetadata(new DateTime(), getActiveEmployeeIdentifier()
                    ,getAirFiApplication().getTabletId(),getFlightMetadata().get().getFlightKey()
                    ,flightLegIdentifier, new DateTime(), level,
                    location != null ? String.valueOf(location.getLatitude()) : null,
                    location != null ? String.valueOf(location.getLongitude()) : null, null, level, null, null);

getFlightMetadata().get().getFlightKey() will be null but how can I add something similar to null check handle this?

Baabidi
  • 13
  • 2

1 Answers1

-1

If you want to handle both the condition with the same piece of code you can do something like this

  String value="";
    try{
             value = getFlightMetadata().get().getFlightKey();
        }catch (NullPointerException e)
        {

        }

now, relace getFlightMetadata().get().getFlightKey() this with value and it will not be null instead it will have blank value or no value but will not be null.

Darshan Soni
  • 1,779
  • 1
  • 12
  • 24
  • it's not good solution. catching NPE is something to be avoided. – Vladyslav Matviienko Jan 18 '17 at 09:35
  • yes but by the information he has provided or the way he has coded this looks the better fix as if it won't has any value then it will be null or it will has some value. So you should have to check for the occurrence of null pointer exception – Darshan Soni Jan 18 '17 at 09:38
  • Here is my question, which is similar to this one. http://stackoverflow.com/questions/40991307/try-catch-vs-long-if. – Vladyslav Matviienko Jan 18 '17 at 09:57