marcojak / MauiMTAdmob

MIT License
115 stars 18 forks source link

Error: Object reference not set to an instance of an object at Plugin.MauiMTAdmob.MauiMTAdmob.GetRequest() #72

Open ernstrogers opened 8 months ago

ernstrogers commented 8 months ago

I have an app built with your plugin version 1.3.2 and the ads work perfect on Android, but when I build it for iOS (physical device, windows machine) it crashes when executing this line of code in my MainPage's constructor:

CrossMauiMTAdmob.Current.LoadInterstitial(INTERSTITIALID);

Here is the error I get:

[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.MauiMTAdmob.MauiMTAdmob.GetRequest() at Plugin.MauiMTAdmob.Platforms.iOS.InterstitialService.CreateInterstitialAd(String adUnit) at Plugin.MauiMTAdmob.Platforms.iOS.InterstitialService.LoadInterstitial(String adUnit) at Plugin.MauiMTAdmob.MauiMTAdmob.LoadInterstitial(String adUnit) at Sharkbytes.Views.HomePage..ctor() in D:\dev\GIT\Sharkbytes\Views\HomePage.xaml.cs:line 29 at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr args) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier) at System.Collections.Concurrent.ConcurrentDictionary2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.b__0() at Microsoft.Maui.Controls.ElementTemplate.CreateContent() at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container) at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent() at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers() at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad() at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at Sharkbytes.Program.Main(String[] args) in D:\dev\GIT\Sharkbytes\Platforms\iOS\Program.cs:line 10 at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr args) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)' The app has been terminated.

ernstrogers commented 7 months ago

I updated my licensed plugin version 1.4.1 and I'm still getting a crash when calling: CrossMauiMTAdmob.Current.LoadInterstitial(INTERSTITIALID);

Here is the error: [0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.MauiMTAdmob.MauiMTAdmob.GetRequest() at Plugin.MauiMTAdmob.Platforms.iOS.InterstitialService.LoadInterstitial(String adUnit) at Plugin.MauiMTAdmob.MauiMTAdmob.LoadInterstitial(String adUnit) at Sharkbytes.Views.HomePage.Ads_Randomizer() in D:\dev\GIT\Sharkbytes\Views\HomePage.xaml.cs:line 66 at Sharkbytes.Views.HomePage.Button_Clicked_Shark_Identification(Object sender, EventArgs e) in D:\dev\GIT\Sharkbytes\Views\HomePage.xaml.cs:line 73 at System.Threading.Tasks.Task.<>c.b__128_0(Object state) at Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:line 176 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at Sharkbytes.Program.Main(String[] args) in D:\dev\GIT\Sharkbytes\Platforms\iOS\Program.cs:line 10 at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)' The app has been terminated.

ernstrogers commented 7 months ago

I also cloned your repo and built the Sample project to my iPhone and it crashes on launch.

marcojak commented 7 months ago

Sorry for the issue, I'm investigating it. Does it crash with every iOS device or just that particular device? Any additional info you could provide will help me to identify a solution.

ernstrogers commented 7 months ago

Yes it crashes with every iOS device. Devices are running iOS 17.3.1, one is iPhone 14, other is iPhone 13.

The app launches when I comment out the line: CrossMauiMTAdmob.Current.LoadInterstitial(INTERSTITIALID);

Here are some lines from the .proj file: <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.0</SupportedOSPlatformVersion>

<PropertyGroup Condition="'$(TargetFramework)'=='net8.0-ios'">

    <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
        <!--<PackageReference Include="Xamarin.Firebase.iOS.Core" Version="8.10.0.3" />-->
        <PackageReference Include="Xamarin.Google.iOS.MobileAds" Version="8.13.0.3" />
        <PackageReference Include="MT.Xamarin.Google.iOS.UserMessagingPlatform" Version="1.1.0.4" />
        <BundleResource Include="GoogleService-Info.plist" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.7" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.7" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="Plugin.Firebase.CloudMessaging" Version="2.0.4" />
        <PackageReference Include="Plugin.MauiMTAdmob" Version="1.4.1" />
        <PackageReference Include="Xamarin.Build.Download" Version="0.11.4" />
    </ItemGroup>

Screenshot 1

marcojak commented 7 months ago

I've just released version 1.4.2. It should fix the issue or at least give more details in the Console output window!

ernstrogers commented 7 months ago

Using version 1.4.2 has fixed the crashing issue and when I call the CrossMauiMTAdmob.Current.LoadInterstitial(INTERSTITIALID);

The ad doesn't load and give just this line in the output window: 2024-04-13 21:43:56.820 Xamarin.PreBuilt.iOS[7891:439756] Ad Request is null. Cannot generate a new request

image

ernstrogers commented 6 months ago

Any thoughts on why the Ad Request is null?

marcojak commented 6 months ago

Hi Ernst,

That message appears when

var request = Request.GetDefaultRequest();

is null.

This is a method from Google.MobileAds so the issue should be there but it's very strange as I've never experienced this in any apps.

This request is the same for interstitials, rewarded interstitials, and banners. Do you see the other ads or do you have problems just with the interstitials?

If you don't see any ads, I think the issue can be some sort of incompatibility in some iOS versions (no one else is reporting this issue).

If the issue happens only with the interstitials, then I would investigate the way you load an interstitial.

The first suggestion would be to call it from the Main Thread. The second could be to check the version of the packages related to the Ads.

Let me know if you have other details that could help me to investigate the issue and I'll try to help you.

Best Regards, Marco

On Tue, Apr 30, 2024 at 5:05 PM Ernst Rogers @.***> wrote:

Any thoughts on why the Ad Request is null?

— Reply to this email directly, view it on GitHub https://github.com/marcojak/MauiMTAdmob/issues/72#issuecomment-2085585486, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAUIPITQDRTQNP3AZYB4N7TY76XL3AVCNFSM6AAAAABFA2YLYOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBVGU4DKNBYGY . You are receiving this because you commented.Message ID: @.***>

ernstrogers commented 6 months ago

Marco, thanks so very much for your library and amazing help!

Since I wasn't able to get your MauiMTAdmob to build for iOS locally, I created my own stripped down sample project based on your MauiMTAdmob project. It uses the same Admob IDs so I can be consistent as possible. I set it to use your 1.4.2 library version. It builds, runs, and displays Interstitial ads perfectly for my Samsung device running Android OS 33. However, when I build it for my iPhone device running iOS 17.3.1 it gives the error I mentioned above.

Can you download the repo and test it out and let me know if it works on your iOS device?

Here is the repo: https://github.com/ernstrogers/AdMobTest Once you have it I was planning to make it private again so I don't confuse anyone doing web searches.

Thanks a whole heap, Ernst

marcojak commented 6 months ago

You are very welcome.

I got your code (you can make it private now) and tried it both on the simulator and on an iPad! Both worked just fine and I was able to load and see the interstitial.

I've tested it on a MacBook M3 Pro with MacOs 14.3.1 using Visual Studio for Mac version 17.6.11

XCode version 15.1 (Don't use higher versions right now as they are not fully compatible with MAUI)...I updated to 15.2, Consent stopped working on the simulator, had many other problems, and went back to 15.1!

Simulator OS version 17.2

Physical device: iPad (5th generation) version 17.7.7.

ernstrogers commented 6 months ago

Sounds like my issue may have to do with using VS for Windows.

Microsoft Visual Studio Community 2022 Version 17.9.0 Preview 3.0 Microsoft .NET FrameworkVersion 4.8.09032

I'm setting my Macbook up with XCode 15.1 so I can build and test there.