question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App crashing with null object[0] in iOS

See original GitHub issue

I am using rewarded and interstitial ads. It is failing in initialisation itself. Also it is mentioning to mention test device id which I already specified as per example and device if as per log.I followed all the steps as mentioned in pub.Dev

Android it is working fine.

Plugin version

0.13.0

Steps to reproduce

Unable to initialise itself

  1. Run flutter create bug.
  2. Update the files as follows: …

Expected results: app should open Actual results: failed with adding null ad. If I comment ads the app is working fine.no issues in android.

Logs

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:11

github_iconTop GitHub Comments

2reactions
nagarjunvvcommented, May 29, 2021

adding MobileAds.instance.initialize(); in init and WidgetsFlutterBinding.ensureInitialized(); solved the issue.

2reactions
nagarjunvvcommented, Sep 1, 2021

Hello @EArminjon

By the way I am using Macbook M1 chip

Sorry i just copied the code from my main code. Below is the formatted code.

  InterstitialAd? _interstitialAd;
  RewardedAd? _rewardedAd;
  int _numInterstitialLoadAttempts = 0;
  int maxFailedLoadAttempts = 5;
  int _numRewardedLoadAttempts = 0;

  void _createInterstitialAd() {
    InterstitialAd.load(
        adUnitId: InterstitialAd.testAdUnitId,
        request: request,
        adLoadCallback: InterstitialAdLoadCallback(
          onAdLoaded: (InterstitialAd ad) {
            print('$ad loaded');
            _interstitialAd = ad;
            _numInterstitialLoadAttempts = 0;
          },
          onAdFailedToLoad: (LoadAdError error) {
            print('InterstitialAd failed to load: $error.');
            _numInterstitialLoadAttempts += 1;
            _interstitialAd = null;
            if (_numInterstitialLoadAttempts <= maxFailedLoadAttempts) {
              _createInterstitialAd();
            }
          },
        ));
  }

  void _showInterstitialAd() {
    if (_interstitialAd == null) {
      print('Warning: attempt to show interstitial before loaded.');
      return;
    }
    _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (InterstitialAd ad) =>
          print('ad onAdShowedFullScreenContent.'),
      onAdDismissedFullScreenContent: (InterstitialAd ad) {
        print('$ad onAdDismissedFullScreenContent.');
        ad.dispose();
        _createInterstitialAd();
      },
      onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
        print('$ad onAdFailedToShowFullScreenContent: $error');
        ad.dispose();
        _createInterstitialAd();
      },
    );
    _interstitialAd!.show();
    _interstitialAd = null;
  }

  void _createRewardedAd() {
    RewardedAd.load(
        adUnitId: RewardedAd.testAdUnitId,
        request: request,
        rewardedAdLoadCallback: RewardedAdLoadCallback(
          onAdLoaded: (RewardedAd ad) {
            print('$ad loaded.');
            _rewardedAd = ad;
            _numRewardedLoadAttempts = 0;
          },
          onAdFailedToLoad: (LoadAdError error) {
            print('RewardedAd failed to load: $error');
            _rewardedAd = null;
            _numRewardedLoadAttempts += 1;
            if (_numRewardedLoadAttempts <= maxFailedLoadAttempts) {
              _createRewardedAd();
            }
          },
        ));
  }

 
  void _showRewardedAd() {
    if (_rewardedAd == null) {
      print('Warning: attempt to show rewarded before loaded.');
      return;
    }
    _rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (RewardedAd ad) =>
          print('ad onAdShowedFullScreenContent.'),
      onAdDismissedFullScreenContent: (RewardedAd ad) {
        print('$ad onAdDismissedFullScreenContent.');
        ad.dispose();
        _createRewardedAd();
      },
      onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
        print('$ad onAdFailedToShowFullScreenContent: $error');
        ad.dispose();
        _createRewardedAd();
      },
    );
    _rewardedAd!.show(onUserEarnedReward: (RewardedAd ad, RewardItem reward) {
      // print('$ad with reward $RewardItem(${reward.amount}, ${reward.type}');
      if (get_votes > 0) {
        setState(() {
          votes_available = votes_available + 1;
          get_votes = get_votes - 1;
        });
      }
    });
    _rewardedAd = null;
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

App crashing in iOS 11.3 with error object cannot be nil (key
I have a web based application that needs to check the current textInputMode. In earlier iOS versions code was working properly, but the...
Read more >
iOS : application crashing for null values while displaying ...
I am receiving JSON response from server, most of the values are NULL , I have written these lines of code, assuming that...
Read more >
Xamarin iOS Try Catch blocks crash app instead of catching
I've noticed if I replace the code attempting to access a property of null object with "throw new NullReferenceException();" The catch will ...
Read more >
Crash in ListView (Null object reference)
My app crashed and this is the logcat output. It appears to be an issue in the ListView item selection code. FATAL EXCEPTION:...
Read more >
Detect and diagnose crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an ... The purpose of null is to signify that the object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found