1

In Android,When I tried to load the Ad in Admob and there is no internet connection the code reaches onRewardedVideoAdFailedToLoad() and after sometime say 30 sec the app force closes with the below mentioned error.

I hope this is not the null pointer exception that I can handle. It is happening in the SDK I guess. Please let me know how to resolve this.

Code

private void setRewardedVideo() {
    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedVideoAd.setUserId(REWARD);
    AdRequest adRequest = new AdRequest.Builder().build();
    rewardedVideoAd.loadAd(AD_UNIT_ID_REWARDED_VIDEO_AD, adRequest);

    rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            System.out.println("onRewardedVideoAdLoaded()");
            if (rewardedVideoAd.isLoaded()) {
                rewardedVideoAd.show();
            }
        }

        @Override
        public void onRewardedVideoAdOpened() {
            System.out.println("onRewardedVideoAdOpened()");

        }

        @Override
        public void onRewardedVideoStarted() {
            System.out.println("onRewardedVideoStarted()");

        }

        @Override
        public void onRewardedVideoAdClosed() {
            System.out.println("onRewardedVideoAdClosed()");

        }

        @Override
        public void onRewarded(RewardItem rewardItem) {
            System.out.println("onRewarded()");

        }

        @Override
        public void onRewardedVideoAdLeftApplication() {
            System.out.println("onRewardedVideoAdLeftApplication()");

        }

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {
            System.out.println("onRewardedVideoAdFailedToLoad()");

        }
    });
}

Logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent com.google.android.gms.ads.internal.au.getParent()' on a null object reference
        at com.google.android.gms.ads.internal.a.b(SourceFile:513)
        at com.google.android.gms.ads.internal.b.b(SourceFile:318)
        at com.google.android.gms.ads.internal.a.c(SourceFile:520)
        at com.google.android.gms.ads.internal.aq.run(SourceFile:64)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5296)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
iappmaker
  • 2,945
  • 9
  • 35
  • 76
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Jan 29 '16 at 13:42

1 Answers1

1

Ensure the user has connectivity before even trying to load the ads, there may be some deeper issue with AdMob that forces this NPE despite no internet.

private void setRewardedVideo() {
    if (!isNetworkAvailable()) {
        // Log no internet
        return;
    }

    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedVideoAd.setUserId(REWARD);
    ....
}

It may even make sense to ensure user has internet before trying to use adMob incase it is something with their library that is causing the issue. See this answer for that.

Community
  • 1
  • 1
DominicEU
  • 3,585
  • 2
  • 21
  • 32
  • I mentioned that there is no internet connection and hence the onRewardedVideoAdFailedToLoad() is getting called. onRewardedVideoAdLoaded() will not be called at all – iappmaker Jan 29 '16 at 13:51
  • Which is why I made an additional comment of ensure that there is an internet connection before even trying to load an ad. It may be some deeper issue with AdMob that is trying to do something despite the lack of connection. – DominicEU Jan 29 '16 at 13:52
  • When there is internet connection rewardVideoAd !=null check is not needed I guess.. And I was not getting any error there – iappmaker Jan 29 '16 at 13:53
  • I've updated my answer to try and examplify what I mean by ensuring there is an internet connection before setting the reward video, using the ```isNetworkAvailable``` method, from the linked answer. – DominicEU Jan 29 '16 at 13:56
  • Thanks This will work. But I need to understand why it is failing and what is wrong in my code. – iappmaker Jan 29 '16 at 13:59