I have the following code. It's about calling an App Open Ad. I have three problems: The first one is that  when I open the app for the first time, it does not shows the ad. In the log, it only shows "AppOpenAdManager - Ad was loaded.". My second problem is that when I open the app for the second time, it shows the ad, but in the log: "NullPointerException occurs when invoking a method from a delegating listener.
                                                  java.lang.NullPointerException: Context cannot be null.
                                                  	at com.google.android.gms.common.internal.Preconditions.checkNotNull(com.google.android.gms:play-services-basement@@18.2.0:2)
                                                  	at com.google.android.gms.ads.appopen.AppOpenAd.load(com.google.android.gms:play-services-ads-lite@@22.6.0:1)
                                                  	at com.example.myapplication.MyApplication$AppOpenAdManager.loadAd(MyApplication.java:70)
                                                  	at com.example.myapplication.MyApplication$AppOpenAdManager.access$300(MyApplication.java:50)
                                                  	at com.example.myapplication.MyApplication$AppOpenAdManager$2.onAdDismissedFullScreenContent(MyApplication.java:117)
                                                  	at com.google.android.gms.internal.ads.zzaxg.zzc(com.google.android.gms:play-services-ads-lite@@22.6.0:1)
                                                  	at com.google.android.gms.internal.ads.zzezl.zza(Unknown Source:2)
                                                  	at com.google.android.gms.internal.ads.zzfaz.zza(com.google.android.gms:play-services-ads@@22.6.0:2)
                                                  	at com.google.android.gms.internal.ads.zzezs.zzj(com.google.android.gms:play-services-ads@@22.6.0:4)
                                                  	at com.google.android.gms.internal.ads.zzfaa.zzq(com.google.android.gms:play-services-ads@@22.6.0:2)
                                                  	at com.google.android.gms.internal.ads.zzfaa.zza(com.google.android.gms:play-services-ads@@22.6.0:1)
                                                  	at com.google.android.gms.internal.ads.zzbju.zza(com.google.android.gms:play-services-ads@@22.6.0:3)
                                                  	at com.google.android.gms.internal.ads.zzchc.zzP(com.google.android.gms:play-services-ads@@22.6.0:6)
                                                  	at com.google.android.gms.internal.ads.zzchc.zzj(com.google.android.gms:play-services-ads@@22.6.0:16)
                                                  	at com.google.android.gms.internal.ads.zzchv.zza(com.google.android.gms:play-services-ads@@22.6.0:3)
                                                  	at com.google.android.gms.internal.ads.zzcht.run(Unknown Source:4)
                                                  	at android.os.Handler.handleCallback(Handler.java:958)
                                                  	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                  	at com.google.android.gms.internal.ads.zzfqv.zza(com.google.android.gms:play-services-ads-lite@@22.6.0:1)
                                                  	at com.google.android.gms.ads.internal.util.zzf.zza(com.google.android.gms:play-services-ads@@22.6.0:1)
                                                  	at com.google.android.gms.internal.ads.zzfqv.dispatchMessage(com.google.android.gms:play-services-ads-lite@@22.6.0:1)
                                                  	at android.os.Looper.loopOnce(Looper.java:205)
                                                  	at android.os.Looper.loop(Looper.java:294)
                                                  	at android.app.ActivityThread.main(ActivityThread.java:8177)
                                                  	at java.lang.reflect.Method.invoke(Native Method)
                                                  	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                                                  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
A GMSG tried to close something that wasn't an overlay.
#004 The webview is destroyed. Ignoring action.
sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda17@d7b2a91
"
And my third problem is that if I re-open the app, it doesn't shows the ad, instead gives log: "The app open ad is not ready yet."

How can I solve these problems?

The code:
package com.example.myapplication;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.appopen.AppOpenAd;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import java.util.Date;

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks, LifecycleObserver {

    private AppOpenAdManager appOpenAdManager;
    private Activity currentActivity;
    public long loadTime = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        MobileAds.initialize(
                this,
                new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {}
                });
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        appOpenAdManager = new AppOpenAdManager(this);
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    protected void onMoveToForeground() {
        // Show the ad (if available) when the app moves to foreground.
        appOpenAdManager.showAdIfAvailable(currentActivity);
    }

    private class AppOpenAdManager {
        private static final String LOG_TAG = "AppOpenAdManager";
        private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/9257395921";

        private AppOpenAd appOpenAd = null;
        private boolean isLoadingAd = false;
        private boolean isShowingAd = false;

        /** Constructor. */
        public AppOpenAdManager(MyApplication myApplication) {}

        /** Request an ad. */
        private void loadAd(Context context) {
            // We will implement this below.
            if (isLoadingAd || isAdAvailable()) {
                return;
            }

            isLoadingAd = true;
            AdRequest request = new AdRequest.Builder().build();
            AppOpenAd.load(
                    context, AD_UNIT_ID, request,
                    AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
                    new AppOpenAd.AppOpenAdLoadCallback() {
                        @Override
                        public void onAdLoaded(AppOpenAd ad) {
                            // Called when an app open ad has loaded.
                            Log.d(LOG_TAG, "Ad was loaded.");
                            appOpenAd = ad;
                            isLoadingAd = false;
                            loadTime = (new Date()).getTime();
                        }

                        @Override
                        public void onAdFailedToLoad(LoadAdError loadAdError) {
                            // Called when an app open ad has failed to load.
                            Log.d(LOG_TAG, loadAdError.getMessage());
                            isLoadingAd = false;
                        }
                    });
        }
        public void showAdIfAvailable(
                @NonNull final Activity activity){
            // If the app open ad is already showing, do not show the ad again.
            if (isShowingAd) {
                Log.d(LOG_TAG, "The app open ad is already showing.");
                return;
            }

            // If the app open ad is not available yet, invoke the callback then load the ad.
            if (!isAdAvailable()) {
                Log.d(LOG_TAG, "The app open ad is not ready yet.");
                loadAd(MyApplication.this);
                return;
            }

            appOpenAd.setFullScreenContentCallback(
                    new FullScreenContentCallback (){

                @Override
                public void onAdDismissedFullScreenContent() {
                    // Called when fullscreen content is dismissed.
                    // Set the reference to null so isAdAvailable() returns false.
                    Log.d(LOG_TAG, "Ad dismissed fullscreen content.");
                    appOpenAd = null;
                    isShowingAd = false;

                    loadAd(activity);
                }

                @Override
                public void onAdFailedToShowFullScreenContent(AdError adError) {
                    // Called when fullscreen content failed to show.
                    // Set the reference to null so isAdAvailable() returns false.
                    Log.d(LOG_TAG, adError.getMessage());
                    appOpenAd = null;
                    isShowingAd = false;

                    loadAd(activity);
                }

                @Override
                public void onAdShowedFullScreenContent() {
                    // Called when fullscreen content is shown.
                    Log.d(LOG_TAG, "Ad showed fullscreen content.");
                }
            });
            isShowingAd = true;
            appOpenAd.show(activity);
        }
        /** Check if ad exists and can be shown. */
        private boolean isAdAvailable() {
            return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
        }
    }
    private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
        long dateDifference = (new Date()).getTime() - this.loadTime;
        long numMilliSecondsPerHour = 3600000;
        return (dateDifference < (numMilliSecondsPerHour * numHours));
    }
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

    @Override
    public void onActivityStarted(Activity activity) {
        // Updating the currentActivity only when an ad is not showing.
        if (!appOpenAdManager.isShowingAd) {
            currentActivity = activity;
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {}

    @Override
    public void onActivityStopped(Activity activity) {}

    @Override
    public void onActivityPaused(Activity activity) {}

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}

    @Override
    public void onActivityDestroyed(Activity activity) {}
}