React-native-branch-deep-linking-attribution: Android NullPointerException crash on start

Created on 9 Dec 2017  路  9Comments  路  Source: BranchMetrics/react-native-branch-deep-linking-attribution

RN 0.44.3
react-native-branch-deep-linking 2.1.1

12-09 03:29:15.534 26919-26919/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.x, PID: 26919
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x/com.x.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.json.JSONObject.getBoolean(java.lang.String)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:158)
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.json.JSONObject.getBoolean(java.lang.String)' on a null object reference
                                                       at io.branch.rnbranch.RNBranchModule$1.onInitFinished(RNBranchModule.java:88)
                                                       at io.branch.referral.Branch.initializeSession(Branch.java:2181)
                                                       at io.branch.referral.Branch.initUserSessionInternal(Branch.java:1233)
                                                       at io.branch.referral.Branch.initSession(Branch.java:890)
                                                       at io.branch.referral.Branch.initSession(Branch.java:964)
                                                       at io.branch.rnbranch.RNBranchModule.initSession(RNBranchModule.java:74)
                                                       at com.x.MainActivity.onStart(MainActivity.java:46)
                                                       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265)
                                                       at android.app.Activity.performStart(Activity.java:6915)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)聽
                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:221)聽
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)聽
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)聽
                                                       at android.os.Looper.loop(Looper.java:158)聽
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224)聽
                                                       at java.lang.reflect.Method.invoke(Native Method)聽
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)聽
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
```聽

package com.gostudentmobile;

import com.mehcode.reactnative.splashscreen.SplashScreen;
import com.mehcode.reactnative.splashscreen.SplashScreenPackage;
import com.imagepicker.ImagePickerPackage;
import io.branch.rnbranch.*;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;

import android.graphics.Color;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends ReactActivity {

/**
 * Returns the name of the main component registered from JavaScript.
 * This is used to schedule rendering of the component.
 */
@Override
protected String getMainComponentName() {
    return "GoStudent";
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Show the js-controlled splash screen
    SplashScreen.show(this, getReactInstanceManager());
    // After react is initialized; set our background color (override splash screen theme)
    getReactNativeHost().getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
        @Override
        public void onReactContextInitialized(ReactContext context) {
            // Hide the native splash screen
            getWindow().getDecorView().setBackgroundColor(Color.WHITE);
      }
    });

    super.onCreate(savedInstanceState);
}

@Override
protected void onStart() {
    super.onStart();
    RNBranchModule.initSession(getIntent().getData(), this);
}

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
}

}
```

I get this upon startup on a new branch sdk install. Any ideas?

Most helpful comment

I am also facing the same issue. What is the fix for this?

All 9 comments

It looks like referringParams is coming in null into onInitFinished() inside RNBranchModule.java. I added a null check in the class to resolve it. Though I'm not sure if this will have any unintended side-effects. The app starts now but I haven't actually tried using it. I'll try that next.

            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                ...
                try{
                    boolean clickedBranchLink = false;
                    String referringLink = null;

                    if (referringParams != null) { // <--- I ADDED THIS.
                        try {
                            clickedBranchLink = referringParams.getBoolean("+clicked_branch_link");
                        }
                        catch (JSONException e) {

                        }
                        ...
                    }
                } catch(JSONException ex) {
                    try {
                        result.put("error", "Failed to convert result to JSONObject: " + ex.getMessage());
                    } catch(JSONException k) {}
                }
                ...
            }

This was due to me not setting up the AndroidManifest.xml file correctly. I had tried to follow the docs.branch.io react-native setup page and the instructions there weren't very clear. I was able to figure it out using the instructions in the readme here on github and by reading about Activities and Intents on the Android API Guides.

It might be a good idea to keep the docs site updated, or at least provide a link back to the readme here.

I am also facing the same issue. What is the fix for this?

I am also currently facing this issue. Can you tell what exactly you did wrong in the AndroidManifest?

Make sure to double check your AndroidManifest.xml. You can get this exception if you put your keys and the receiver inside the android:name=".MainActivity"... and inside the application node.

Example end of AndroidManifest.xml



    <!-- Branch testing (TestMode "true" to simulate fresh installs on dev environment) -->
    <meta-data android:name="io.branch.sdk.TestMode" android:value="true" />

    <!-- Branch install referrer tracking -->
    <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>
</application>

Hii ,

In my case i forgot to add
RNBranchModule.initSession(getIntent().getData(), this);

in MainActivity => onStart method.
Please check it .

I've implemented all of the above, but still I'm getting these crashes, any update?

Whenever I've seen this, it's almost always because the Branch key is not set in the manifest. If you're using branch.json, you still have to set something in the manifest, even just key_live_xxxx. I'll double-check and see if we can do something better.

This comment helped me out - https://github.com/BranchMetrics/unity-branch-deep-linking-attribution/issues/171#issuecomment-574704961

I was missing implementation 'com.android.installreferrer:installreferrer:1.1' in my process of migrating from v2.3.5 to v3.1.2, I had followed the steps mentioned in release notes for v3.0.0 where I removed the implementation 'io.branch.sdk.android:library:3.1.2' import, but this step of adding implementation 'com.android.installreferrer:installreferrer:1.1' was missing, I guess we need to update the release notes with this step.

Release notes for v3.0.0 -
https://github.com/BranchMetrics/react-native-branch-deep-linking-attribution/releases/tag/v3.0.0

Thanks for the quick replay @jdee

Was this page helpful?
0 / 5 - 0 ratings