Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Throws NullReferenceException when I try to invoke a function #401

Open
cihadturhan opened this issue Jul 23, 2024 · 9 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@cihadturhan
Copy link

cihadturhan commented Jul 23, 2024

MAX Plugin Version

6.5.2

Unity Version

2022.3.22f1

Device/Platform Info

Android 10 - 11

Current Behavior

The code snippet below throws NullReferenceException when the it fails to load a rewarded ad.

  private void OnLoad(){
      MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
  }
  // ...

  private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
  {
      // Rewarded ad failed to load 
      // AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds).

      retryAttempt++;
      double retryDelay = Math.Pow(2, Math.Min(6, retryAttempt));
      
      Invoke(nameof(LoadRewardedAd), (float)retryDelay); // <<< This line throws error
  }

  private void LoadRewardedAd()
  {
      MaxSdk.LoadRewardedAd(adUnitId);
  }

Expected Behavior

I'd expect Invoke function to be called without issues but it throws NullReferenceException for the called function name.

2024/07/23 12:39:00.221 14308 14422 Error Unity Error [AppLovin MAX] Caught exception in publisher event: OnInterstitialLoadFailedEvent, exception: System.NullReferenceException: Object reference not set to an instance of an object.
2024/07/23 12:39:00.221 14308 14422 Error Unity   at IsAdController.OnInterstitialLoadFailedEvent (System.String adUnitId, MaxSdkBase+ErrorInfo errorInfo) [0x00000] in <00000000000000000000000000000000>:0 
2024/07/23 12:39:00.221 14308 14422 Error Unity   at AppLovinMax.Internal.MaxEventExecutor.Update () [0x00000] in <00000000000000000000000000000000>:0 

How to Reproduce

  • Copy the snippet above
  • Initialize MaxSdk and call OnLoad after it's initialized
  • Build for an android device
  • Turn the network off and launch the game

Additional Info

No response

@cihadturhan cihadturhan added the bug Something isn't working label Jul 23, 2024
@santoshbagadi
Copy link
Contributor

Hi @cihadturhan , the NullReferenceException seems to be happening in OnInterstitialLoadFailedEvent. Could you please check the implementation of that method instead of OnRewardedAdLoadFailedEvent.

@cihadturhan-st
Copy link

Hi @cihadturhan , the NullReferenceException seems to be happening in OnInterstitialLoadFailedEvent. Could you please check the implementation of that method instead of OnRewardedAdLoadFailedEvent.

Hey, that’s my bad when posting here. But the issue is same for both OnRewardedAdLoadFailedEvent

AppLovin callbacks cannot execute Invoke on android. I think this callbacks are called in threads other than main. Is it correct?

In my case, I converted function to static and called it directly instead of Invoke. So it’s resolved but this problem is still there

@JonathanLiuApp
Copy link
Contributor

JonathanLiuApp commented Jul 23, 2024

@cihadturhan Yeah most of the callbacks are called in background threads unless you specify otherwise. Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true; and see if that helps?

@cihadturhan-st
Copy link

Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true;

Wow is there such a variable?
I’ll try that in the next build. I sometimes see random crashes maybe that’s the reason.

Is there any drawbacks of running events on the main thread (ie freezing the ui or network requests)?

@JonathanLiuApp
Copy link
Contributor

It would have the same drawbacks as putting the event in a Unity event function like Start() or Update(). Depending on the code in the callback there could be differences in performance, but for most cases there would likely be no difference.

@cihadturhan
Copy link
Author

Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true; and see if that helps?

Hi Jonathan, kinda related to this issue so I wanted to ask here.
I saw some crashes and some devs reported it's because of the crashes in related to callbacks from Admob are not being called in the main thread.

Example: googleads/googleads-mobile-unity#2854 (comment)

I wonder when I add MaxSdkBase.InvokeEventsOnUnityMainThread, does it call MobileAds.RaiseAdEventsOnUnityMainThread = true for Admob and other libraries too?

@JonathanLiuApp
Copy link
Contributor

MaxSdkBase.InvokeEventsOnUnityMainThread only ensures that our own callbacks from MaxSdkCallbacks.cs are invoked on the main thread. It doesn't set MobileAds.RaiseAdEventsOnUnityMainThread or similar in other libraries so it shouldn't be causing any crashes related to Admob callbacks.

@cihadturhan
Copy link
Author

MaxSdkBase.InvokeEventsOnUnityMainThread only ensures that our own callbacks from MaxSdkCallbacks.cs are invoked on the main thread. It doesn't set MobileAds.RaiseAdEventsOnUnityMainThread or similar in other libraries so it shouldn't be causing any crashes related to Admob callbacks.

Thanks for the response. I'm trying it now. I'm guessing it should be set before initialization of the SDK?
I'm assuming that even the other libraries call MaxSdkBase in other thread, the statement InvokeEventsOnUnityMainThread will eventually call unity callbacks in the main thread.

@JonathanLiuApp
Copy link
Contributor

It is best to set it prior to initialization, otherwise the onSdkInitializedEvent might not be pushed to the main thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants