PopUpButton failing inside ViewStack container

Problem

You have a PopUpButton that is failing with a null error unexpectedly.

Solution

Reinitialize the popUp property of the PopUpButton every time it receives the addedToStage event. You can save a reference to the class in your parent component.

Explanation

The PopUpButton performs some cleanup when it receives the removedFromStage handler because it assumes that it is done for business. However, sometimes the Container class will cause this behavior at runtime unexpectedly. The PopUpButton will be immediately placed back on the Stage, but it has already destroyed its popUp component and thrown away its reference.

  1.     private function removedFromStageHandler(event:Event):void
  2.     {
  3.         // Ensure we've unregistered ourselves from PopupManager, else
  4.         // we'll be leaked.
  5.         if (_popUp) {
  6.             PopUpManager.removePopUp(_popUp);
  7.             _popUp = null;
  8.         }
  9.     }

This wouldn't be such a problem, but the PopUpButton has no way of recovering this component and most of the methods do not check if the popUp property is null. For example, the private function displayPopUp(show:Boolean):void method checks if popUp is null if show is true, but not if it is false:

  1.     private function displayPopUp(show:Boolean):void
  2.     {

...

  1.         if (show)
  2.         {
  3.             if (getPopUp() == null)
  4.                 return;

...

  1.         }
  2.         else
  3.         {
  4.             point = _popUp.parent.globalToLocal(point);

BOOM.