0

I created my app and now I'm trying to add AdMob Ads but I'm stuck with an instant crash and a problem and I don't know how to fix it.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.glitchrun.sapphire/com.glitchrun.sapphire.PTPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest) on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
    at com.glitchrun.sapphire.PTPlayer.onCreate(PTPlayer.java:68)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)

I tried to remove import com.google.android.gms.ads.AdView; but after that ads are not showing in the app. BUT I still have requests in AdMob and matchrate even if ads are not showing. How to solve the problem?

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);`
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • It's a NullPointerException. You just need to make sure you're passing a value when calling `mAdView.loadAd(adRequest);`. Debug and make sure `adRequest` is not null – abhndv Feb 12 '19 at 15:07

1 Answers1

1

Try to read the exception properly. It is a null pointer exception.

java.lang.NullPointerException: Attempt to invoke virtual method void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest) on a null object reference

It says that, loadAd method was called on a null object. That means your AdView object was null. Probably it wasn't correctly initialized or its ID was wrong.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64