facebook / facebook-sdk-for-unity

The facebook sdk for unity.
https://developers.facebook.com/docs/unity
Other
492 stars 257 forks source link

NullReferenceException inside Facebook.Unity.FacebookBase.API #534

Open NeroDocx opened 3 years ago

NeroDocx commented 3 years ago

Checklist

Environment

Goals

I want to get the player ID after login and restart the game.

Expected Results

Сallback should occur in FB.API if FB.IsLoggedIn is true. I want not to get NullReferenceException: Object reference not set to an instance of an object. Facebook.Unity.FacebookBase.API after successful login.

Actual Results

I use the login "FB.Mobile.LoginWithTrackingPreference" on the iOS device. After login, OnLoginResult is called. I restart the game and call "FB.API (" me? Fields = id ", HttpMethod.GET, OnPublicProfileGet)". After that, an error occurs on the device.

stack trace: NullReferenceException: Object reference not set to an instance of an object. Facebook.Unity.FacebookBase.API (System.String query, Facebook.Unity.HttpMethod method, System.Collections.Generic.IDictionary2 [TKey, TValue] formData, Facebook.Unity.FacebookDelegate1 [T] callback) (at <00000000000000000000000000000000>: 0) FacebookManager + d__12.MoveNext () (at <00000000000000000000000000000000>: 0) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <00000000000000000000000000000000>: 0)

Steps to Reproduce

Steps:

Code Samples & Details

public void Initialize() {
    var go = new GameObject();
    DontDestroyOnLoad(go);
    go.name = "FacebookManager";
    go.AddComponent<FacebookManager>();

    FB.Init(AppId, clientToken, true, true, true, false, true, null, "en_US", null, OnInitComplete);
}

private void OnInitComplete() {
    if (FB.IsLoggedIn) {
        FB.LogAppEvent(AppEventName.ActivatedApp);
        StartCoroutine(Instance.UpdatePlayerIdTasks());
    }
}

private IEnumerator UpdatePlayerIdTasks() {
    yield return null;
    if (FB.IsLoggedIn) {
        FB.API("me?fields=id", HttpMethod.GET, OnPublicProfileGet);
    }
}
sebastian-dudzic-spl commented 1 year ago

We're experiencing exactly the same issue. A lot of null refs on prod. Any updates? @NeroDocx was it fixed for you? Or do you have a workaround?

Our version is Unity FB SDK - 15.1

sanuzzi commented 4 months ago

Same here. Unity 2021.3.36f1 Facebook SDK 17.0.1

The error only occurs on iOS On Android it works fine

aliyce commented 3 months ago

Exactly the same issue with @sanuzzi

nanddo commented 2 months ago

Having similar errors in production with the following set-up:

Similarly to @sanuzzi, the issue only happens on iOS, mostly on iPads (as seen in the screenshot below)

Error on Crashlytics:

Non-fatal Exception: NullReferenceException
0  ???                            0x0 API (Facebook.Unity.FacebookBase)

Facebook SDK issue

chanchangame commented 1 month ago

@NeroDocx @sanuzzi @aliyce @nanddo Did you guys find a solution to this issue? I'm facing the exact problem with FB SDK v14.1.0, v15.1.0, v16.0.2 and v17.0.1 and Unity 2022.3.34f1. I have no idea why this is happening. It was working perfectly before.

nanddo commented 1 month ago

@chanchangame No solution yet, the crash keeps happening. The only possibility seems to be removing the Facebook SDK, which is far from ideal.

chanchangame commented 1 month ago

@nanddo I added some logs and it looks like on kill and launch, the AccessToken.CurrentAccessToken becomes null. Maybe FB.API() is using that internally to hit the Graph API. I don't know whether this is an expected behaviour. I checked FB.IsInitialized and FB.IsLoggedIn just before calling FB.API() and both are returning true.

nanddo commented 1 month ago

Hey @chanchangame thanks for the info. Were you able to bypass the null reference somehow (maybe adding a try catch block whenever calling the FB.API()?). Checking for FB.IsInitialized and FB.IsLoggedIn doesn't seem to solve the issue if I understood correctly.

chanchangame commented 1 month ago

@nanddo I found out that it was indeed because of empty access token. I was not able to directly fix it being empty in subsequent sessions, but I found a work around. Before calling FB.API() method, I will check if AccessToken.CurrentAccessToken is null. If it is null, then I will call FB.Mobile.RefreshCurrentAccessToken() provided by the SDK and will wait for the result and then call FB.API(). This way the access token will be refreshed and then the call will work.

But if you don't want to call RefreshCurrentAccessToken() on every session, then you can check out this thread: https://github.com/facebook/facebook-sdk-for-unity/issues/695

I wasn't able to test what @dvgco said at the end because of time constrains. You can give it a try if you want.