I have the following three classes:
class Dom_Form_Section extends Dom {
  /* ... code ommited ... */
  public function addElem($Elem) {
      if (is_a($Elem, 'Dom_Form_Elem')) $FormElem=$Elem;
      else $FormElem=Dom_Form_Elem::create(array(), $Elem);
      if ($FormElem !== false) $this->FormElems[]=$FormElem;
      return $FormElem;
  }
}
class Dom_Form extends Dom {
   private $FormSections=array();
   /* ... code ommited ... */
   public function addElem($Elem) {
    if (is_a($Elem, 'Dom_Form_Elem')) $FormElem=$Elem;
    else $FormElem=Dom_Form_Elem::create(array(), $Elem);
    if ($FormElem !== false) {
        if (empty($this->FormSections)) $Section=$this->addSection();
        else $Section=$this->FormSections[count($this->FormSections)];
        return $Section->addElem($FormElem); // !!! this is where the error fires
    } else return false;
   }
   public function addSection($SectionData=array()) {
    $id=$this->FormId."-section-".count($this->FormSections);
    if (!is_array($SectionData)) $SectionData=array();
    $FormSection=new Dom_Form_Section($SectionData, $id);
    $this->FormSections[]=$FormSection;
    return $FormSection;
 }
}
class Dom_Form_Elem extends Dom {
  public static function create($data, $Elem) {
    if (!is_a($Elem, 'Dom')) return false;
    else {
       $FormElem=new Dom_Form_Elem($data, $Elem);
       return $FormElem;
    }
  }
  /* ... code ommited ... */
}
If I run the following code:
 $Form=new Dom_Form();
 $Form->addElem($Input); // $Input is of 'Dom'
I get the following error:
Fatal error: Call to a member function addElem() on null
If I include some echoes in the two addElem functions (the one in Dom_Form_Section and the one in Dom_Form) they both fire, but the error still persists. It looks to me as if I am making a loop somewhere and that's why I get the error.
Additionally, if I var_dump the contents of the $Section variable, just before the error fires, it is a valid Dom_Form_Section object. The error fires when I try to call the Dom_Form_Section::addElem() method.
What could be wrong with the code?
EDIT: 
With the help of @A-2-A I've figured out that the problem was with this line: 
else $Section=$this->FormSections[count($this->FormSections)];
I've tried to access an undeclared member of the $this->FormSections array. By changing count($this->FormSections) to count($this->FormSections)-1 the code now works fine.
 
     
    