0

Every time I'm assigning getSystemService to some variable I have to check later if it is null, eg.

LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager != null) {...}

Is there any way to make it non null for sure?

Nemanja
  • 211
  • 6
  • 16
  • One way to do that is by writing `getSystemService()` in such a way that it never returns a null object (i.e. throwing if it cannot create one). Of course, you would then have to wrap the call in a `catch` block. If you don't own the `getSystemService` code, then there's nothing more you can do. – Robert Harvey Aug 31 '18 at 18:13
  • So overall, I'd say that you're probably already doing the best you can. – Robert Harvey Aug 31 '18 at 18:15
  • I don't think I've ever seen location manager come back null. Did you add permissions? – Gabe Sechan Aug 31 '18 at 18:28
  • Ofc I did, and it never came as null to me, but it is just a possibility, android studio is warning me about it so I was wandering... On the other hand this one `NotificationManager mNotificationManager = getSystemService(NotificationManager.class);` did crash on me once after hundresds/thousands of launches – Nemanja Aug 31 '18 at 19:59

0 Answers0