BranchMetrics / unity-branch-deep-linking-attribution

The Branch Unity SDK for deep linking and attribution. Branch helps mobile apps grow with deep links / deeplinks that power paid acquisition and re-engagement campaigns, referral programs, content sharing, deep linked emails, smart banners, custom user onboarding, and more.
https://docs.branch.io/apps/unity/
MIT License
118 stars 25 forks source link

java.lang.NullPointerException when debugging with adb #46

Closed Acimaz closed 8 years ago

Acimaz commented 8 years ago

Hey there, this is part of the output when i use adb for debugging my app:

09-13 16:50:33.340 28812-28812/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:464) at io.branch.unity.BranchUnityWrapper.setMaxRetries(BranchUnityWrapper.java:289) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy3.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.re 09-13 16:50:33.360 28812-28812/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:464) at io.branch.unity.BranchUnityWrapper.setNetworkTimeout(BranchUnityWrapper.java:293) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy3.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lan 09-13 16:50:33.380 28812-28812/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:464) at io.branch.unity.BranchUnityWrapper.setRetryInterval(BranchUnityWrapper.java:285) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy3.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang

This doesnt break the app and branch still seems to work, but maybe this should be fixed. I dont know what it does and what is missing, maybe you know it.

antonargunov commented 8 years ago

Hi @Acimaz , What is adb? Do you have only Branch SDK in that your project? May be you included something else?

I just need info to reproduce that issue.

Acimaz commented 8 years ago

Hey @antonargunov adb is the android debug bridge which you can use to debug android stuff directly from your phone. Hm, in that build i could have included the facebook sdk as well i think. Still that should not happen because branch and facebook sdk are used together very often.

Acimaz commented 8 years ago

You can start adb by typing the following into the command line: adb logcat -s Unity This filters everything but unity messages. Or you can use the built in adb tool in android studio. Sometimes its really helpful because you cant debug.log the java stuff into unity or at runtime without using SendMessage from java side

antonargunov commented 8 years ago

@Acimaz Usually I use android studio, I will check in android studio at first.

Acimaz commented 8 years ago

Its called logcat

logcat

antonargunov commented 8 years ago

@Acimaz I think I know what happens. Try please to update UnitySDK or just download android lib and update. I placed in UnitySDK pure android lib without additional dependences.

Acimaz commented 8 years ago

@antonargunov Do you mean the new Branch-2.3.0.jar file you pushed to master?

antonargunov commented 8 years ago

@Acimaz yes

Acimaz commented 8 years ago

@antonargunov Alright anton i updated my files but the issue still persists.

But i could find out why it happens:

When i watch the adb output the first line says the following:

09-15 10:21:46.724 23648-23648/? I/BranchSDK: Branch Warning: Please enter your branch_key in your project's Manifest file!

and then:

09-15 10:21:48.816 23648-23648/? I/BranchSDK: Branch Warning: Please enter your branch_key in your project's res/values/strings.xml!

The strange thing is that i have no problems in tracking my events on the branch dashboard. But when you look at the method from this error message:

09-15 10:21:56.844 23648-23648/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:466)

It says that this method "startsWith()" is used in Branch.getInstance(). Well, i took a look inside this class and method and saw the following:

public static Branch getInstance(@NonNull Context context, @NonNull String branchKey) { if (branchReferral_ == null) { branchReferral_ = Branch.initInstance(context); } branchReferral_.context_ = context.getApplicationContext(); if (**branchKey.startsWith("key_")**) { boolean isNewBranchKeySet = branchReferral_.prefHelper_.setBranchKey(branchKey); //on setting a new key clear link cache and pending requests if (isNewBranchKeySet) { branchReferral_.linkCache_.clear(); branchReferral_.requestQueue_.clear(); } } else { Log.e("BranchSDK", "Branch Key is invalid.Please check your BranchKey"); } return branchReferral

As you can see, it uses the key_ var which is null at this moment of method call because of the first line i posted and then it causes a null pointer exception.

I hope this information is helpful.

antonargunov commented 8 years ago

@Acimaz thanks! I know what happens. It will be fixed today.

Acimaz commented 8 years ago

@antonargunov You are awesome, thank you :+1:

antonargunov commented 8 years ago

HI @Acimaz , download please last version of UnitySDK from github. If you will have any troubles - feel free to open new issue.

Acimaz commented 8 years ago

Hey @antonargunov, the NPE is still present and can be seen via adb in android studio:

09-21 12:59:10.082 30753-30753/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:466) at io.branch.unity.BranchUnityWrapper.setMaxRetries(BranchUnityWrapper.java:289) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy2.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.re 09-21 12:59:10.092 30753-30753/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:466) at io.branch.unity.BranchUnityWrapper.setNetworkTimeout(BranchUnityWrapper.java:293) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy2.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lan 09-21 12:59:10.112 30753-30753/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference at io.branch.referral.Branch.getInstance(Branch.java:466) at io.branch.unity.BranchUnityWrapper.setRetryInterval(BranchUnityWrapper.java:285) at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method) at com.unity3d.player.ReflectionHelper.a(Unknown Source) at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy2.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang

Acimaz commented 8 years ago

@antonargunov nvm! I called these 3 methods in Awake: Branch.setMaxRetries(5); Branch.setNetworkTimeout(10); Branch.setRetryInterval(3);

But moving them over to Start() resolved the problem.

Mann1ng commented 5 years ago

Glad to have found this... I updated the Start() with Awake() for Branch to get in early, but didn't realise this would cause an exception. Might be worth adding a comment above the Start() to warn developers to not update to Awake() ;)