-1

I have a quite vanilla service that works seamlessly when the app is executed on my LG device. Yet when I do it on the Eclipse emulator for the Nexus 5 I get a crash, leaving me with the suspect this behavior could also emerge on real devices different from mine. This is the code:

Intent startBackgroundLocationIntent= new Intent(this, BackgroundLocationService.class);
startService(startBackgroundLocationIntent);

and the crash log:

08-13 14:18:10.731: E/AndroidRuntime(6345): FATAL EXCEPTION: main 08-13 14:18:10.731: E/AndroidRuntime(6345): Process: com.example.taxiprofessional, PID: 6345 08-13 14:18:10.731: E/AndroidRuntime(6345): java.lang.RuntimeException: Unable to start service com.example.taxiprofessional.BackgroundLocationService@b20e39b0 with Intent { cmp=com.example.taxiprofessional/.BackgroundLocationService }: java.lang.NullPointerException 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.app.ActivityThread.access$2100(ActivityThread.java:135) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.os.Handler.dispatchMessage(Handler.java:102) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.os.Looper.loop(Looper.java:136) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-13 14:18:10.731: E/AndroidRuntime(6345): at java.lang.reflect.Method.invokeNative(Native Method) 08-13 14:18:10.731: E/AndroidRuntime(6345): at java.lang.reflect.Method.invoke(Method.java:515) 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-13 14:18:10.731: E/AndroidRuntime(6345): at dalvik.system.NativeStart.main(Native Method) 08-13 14:18:10.731: E/AndroidRuntime(6345): Caused by: java.lang.NullPointerException 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100) 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.example.taxiprofessional.BackgroundLocationService.onStartCommand(BackgroundLocationService.java:138) 08-13 14:18:10.731: E/AndroidRuntime(6345): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2702) 08-13 14:18:10.731: E/AndroidRuntime(6345): ... 10 more

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • paste the line 100 and 138 of class BackgroundLocationService.java – Jamil Aug 13 '14 at 18:38
  • That was the culprit: uploadCoordinatesForLocation(last); when it was null the function crashed, so I protected it with: if (last!=null) . Once the point of the program is found, finding a solution is relatively easy, if functions behave as they should. – Fabrizio Bartolomucci Aug 13 '14 at 21:28

2 Answers2

0

Reading that stack trace, it looks like the code that's crashing is your upload method: E/AndroidRuntime(6345): Caused by: java.lang.NullPointerException 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100) Just speculating here, but have you validated that you're null checking the coordinates that you're trying to upload? Your emulator won't have a location unless you configure mock locations correctly.

sddamico
  • 2,130
  • 16
  • 13
  • Thanks for that, I must say I am not good at all at finding my way in the Android crash logs. I solved the issue by checking the location before executing operations on it. Thanks you all, I hope to become more and more proficient with time in this not so exciting activity. – Fabrizio Bartolomucci Aug 13 '14 at 21:11
0

I fixed the whole issue by checking if variable last was null before calling the function and by requesting the best provider; this is the code snippet I hope to be of use for beginners like me:

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
String provider = lm.getBestProvider(criteria, true); 
if (lm!=null) { 
      Location last=lm.getLastKnownLocation(provider);
      if (last!=null) uploadCoordinatesForLocation(last);  
      lm.requestLocationUpdates(provider, 0, 0, locationListener); 
}

Thanks everyone.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75