0 Replies Latest reply on Oct 21, 2011 7:18 AM by anixan

    Android Air App using AdMob "NULL Object" Error?!?

    anixan

      Hi,

       

      I am currently developing an app using adobe air flashbuilder and I have incorporated AdMob type html adverts into it. The Adverts do come up nicely however if I run the app in debug mode I get this error:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at model::AdMob/createAd()[C:\Users\Jack\Documents\Dropbox\Projects\App\App Final\src\model\AdMob.as:37]
      

       

       

      The Ads do still work regardless of this however this is then affecting other elements within the app so I would like to get rid of the error that is happening. Below is my code in the "AdMob.as" file where I believe the issue to be occuring, can someone please help me with this?

       

      package model
      {
          import flash.display.Stage;
          import flash.events.Event;
          import flash.events.LocationChangeEvent;
          import flash.events.MouseEvent;
          import flash.geom.Rectangle;
          import flash.media.StageWebView;
          import flash.net.URLRequest;
          import flash.net.navigateToURL;
          
          public class AdMob
          {
              // setup variables
              private var _stageWebView:StageWebView;
              private var myAdvertURL:String = "http://www.test.co.uk/AdMob/test.html";
              //private var myAdvertURL:String;
              private var adWidth:Number=480;
              private var adHeight:Number=85;
              private var stage:Stage;
              
              public function AdMob(adURL:String,_stage:Stage)
              {
                  myAdvertURL=adURL;
                  stage=_stage;
                  createAd(); 
              }
              
              public function createAd():void {
                  // check that _stageWebView doersn't exist
                  if (! _stageWebView) {
                      _stageWebView = new StageWebView () ;
                      // set the size of the html 'window'
                      _stageWebView.viewPort = new Rectangle(0,stage.stageHeight-adHeight, 800, adHeight);
                      //_stageWebView.viewPort = new Rectangle((stage.stageWidth-adWidth)/2,stage.stageHeight-adHeight,adWidth,adHeight);
                      // add a listener for when the content of the StageWebView changes
                      _stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,onLocationChange);
                      //add a listener for when the ad is loaded
                      _stageWebView.addEventListener(Event.COMPLETE,onComplete);
                      // start loading the URL;
                      _stageWebView.loadURL(myAdvertURL);
                  }
                  // show the ad by setting it's stage property;
                  //showAd();
              }
              
              public function destroyAd():void {
                  // check that the instace of StageWebView exists
                  if (_stageWebView) {
                      trace("removing advert");
                      // destroys the ad
                      _stageWebView.stage = null;
                      _stageWebView = null;
                  }
              }
              
              public function toggleAd():void {
                  trace("toggling advert",_stageWebView);
                  // check that StageWebView instance exists 
                  if (_stageWebView) {
                      trace("_stageWebView.stage:"+_stageWebView.stage);
                      if (_stageWebView.stage == null) {
                          //show the ad by setting the stage parameter
                          _stageWebView.stage = stage;
                      } else {
                          // hide the ad by nulling the stage parameter
                          _stageWebView.stage = null;
                      }
                  } else {
                      // ad StageWebView doesn't exist - show create it
                      createAd();
                  }
              }
          
              public function onLocationChange(event:LocationChangeEvent):void {
                  // check that it's not our ad URL loading
                  if (_stageWebView.location != myAdvertURL) {
                      // destroy the ad as the user has kindly clicked on my ad
                      destroyAd();
                      // Launch a normal browser window with the captured  URL;
                      navigateToURL( new URLRequest( event.location ) );
                  }
              }
              
              public function onComplete(event:Event):void{
                  //add the ad when it is loaded. If not it will apear a white rectangle until the load is complete.
                  showAd();
              }
              
              public function updateAd(adURL:String=null):void{
                  if (adURL!=null){
                      myAdvertURL = adURL;
                  }
                  destroyAd();
                  createAd();
              }
              
              public function showAd():void{
                  if (_stageWebView) 
                      _stageWebView.stage = stage;
              }
              
              public function hideAd():void{
                  if (_stageWebView) 
                      _stageWebView.stage = null;
              }
              //
          }
      }