1

This is a problem that forked off a different issue.

I'm passing two strings from my main activity to a child service, which is fine, but when the main activity dies, the service throws a NullPointerException trying to grab the two strings.

From MainActivity:

Intent i = new Intent(this, PebbleService.class);
i.putExtra("quote", quote[0]);
i.putExtra("author", quote[1]);
startService(i);

From PebbleService:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    final String author = intent.getStringExtra("author");
    final String quote = intent.getStringExtra("quote");

    // Define AppMessage behavior
    if (appMessageReciever == null) {
        appMessageReciever = new PebbleKit.PebbleDataReceiver(WATCHAPP_UUID) {

            @Override
            public void receiveData(Context context, int transactionId, PebbleDictionary data) {
                // Always ACK
                PebbleKit.sendAckToPebble(context, transactionId);

                // Send KEY_QUOTE to Pebble
                PebbleDictionary out = new PebbleDictionary();
                out.addString(KEY_QUOTE, quote);
                out.addString(KEY_AUTHOR, author);
                PebbleKit.sendDataToPebble(getApplicationContext(), WATCHAPP_UUID, out);
            }
        };

        // Add AppMessage capabilities
        PebbleKit.registerReceivedDataHandler(this, appMessageReciever);
    }

    return START_STICKY;
}

Error from Logcat:

05-26 10:29:52.972    4147-4147/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.thevgc.quotes, PID: 4147
    java.lang.RuntimeException: Unable to start service net.thevgc.quotes.PebbleService@423eb138 with null: java.lang.NullPointerException
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2732)
            at android.app.ActivityThread.access$2100(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at net.thevgc.quotes.PebbleService.onStartCommand(PebbleService.java:34)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2715)
            at android.app.ActivityThread.access$2100(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
Cora
  • 269
  • 3
  • 17

1 Answers1

2

As per the documentation:

intent - ... This may be null if the service is being restarted

Therefore check for null intent:

public int onStartCommand(Intent intent, int flags, int startId) {
    int cmd = super.onStartCommand(intent, flags, startId);

    if (intent == null) return cmd;

    final String author = intent.getStringExtra("author");
    final String quote = intent.getStringExtra("quote");

    ...
}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • So this solves the NPE, which is technically the correct statement. But it also means I don't have access to those strings when the main app isn't running. – Cora May 26 '15 at 17:40
  • @DarkWolffe do you expect your service to be called when your app isn't running? If so, it shouldn't depend on the strings passed to it.. – Simas May 26 '15 at 17:41
  • That's part of the problem: the whole point of the service is to be able to pass the data gathered from the app's classes and pass it to the Pebble regardless of the state of the app (as the app has to be in some way running for the Pebble app to be able to gather data from it). – Cora May 26 '15 at 17:43
  • @DarkWolffe sounds like your app should save the data in preferences or a SQLite database and the service would fetch it. You can't just access the classes like you did previously (randomly instantiating `MainActivity`) or currently because the strings aren't sent to you by anyone while the app isn't running. – Simas May 26 '15 at 17:46
  • I was afraid it would come to that. I think I was just trying to find a way to not have to script a bunch of new stuff. Oh well! Back to work. – Cora May 26 '15 at 17:47
  • 1
    @DarkWolffe if all you need is a few strings Preferences will be more than enough and it's doable with only a few lines of code. In any case, good luck! – Simas May 26 '15 at 17:49