title: Components and aggregations in SAPUI5
body: <p>I nee a Tree as an aggregation in my project. I find something about, how to acces them. But I got still a problem ... Everytime I want to call the method <code>setTitle</code> of the Tree I got an Exception: <code>Uncaught TypeError: Cannot read property 'setTitle' of null</code></p>
  
  <p>My code looks like this:</p>
  
  <pre><code>sap.ui.core.UIComponent.extend("ui5_i.myComponent.Component", {
  
  metadata : {
      includes: ["../../resources/css/org-struct.css"],
      properties : {
          treeTitle: "string",
          searchPlaceholder: "String",
          showId: { type:"boolean", defaultValue: true },
          data: "object",
          height :"string",
          width: "string"
      },
      aggregations: { 
          _Tree : {type:'sap.ui.commons.Tree', multiple: false}
      }
  },
  
  createContent: function(){
          console.log("createContent!");
          this.set_Tree(new sap.ui.commons.Tree());
          ....
          .... here comes the rest of the view
  }
  </code></pre>
  
  <p>And my setter Method:</p>
  
  <pre><code>ui5_i.myComponent.Component.prototype.setTreeTitle = function(sTitle) {
  if(typeof(sTitle) === "string" &amp;&amp; sTitle != undefined &amp;&amp; sTitle !=null){
       this.setProperty("treeTitle", sTitle);
       this.get_Tree().setTitle(sTitle);
       return this;
  }else{
      throw("Property 'treeTitle' is not of type 'string' or 'undefined'!");
  }
  </code></pre>
  
  <p>I don't understand the error, because the content is created, so the tree can't be <code>null</code>, I even see the Tree when I comment the setter method, so I normally can call the setTitle method ...
  Anyone an idea what's going wrong?</p>
  
  <p>EDIT: I realised, that my _Tree is everytime null, but I used the setter to initialize it ... I also changed it and put the init of the tree into my init() method, but than I want to add the tree to the View and get an error like this: <code>Uncaught TypeError: Cannot read property 'addDelegate' of null</code>
  Here is the whole code:</p>
  
  <pre><code>    jQuery.sap.require("sap.ui.core.UIComponent");
  jQuery.sap.require("sap.ui.commons.Tree");
  jQuery.sap.declare("ui5_i.myComponent.Component");
  
  sap.ui.core.UIComponent.extend("ui5_i.myComponent.Component", {
  
      metadata : {
          includes: ["../../resources/css/org-struct.css"],
          properties : {
              treeTitle: "string",
              searchPlaceholder: "String",
              showId: { type:"boolean", defaultValue: true },
              data: "object",
              height :"string",
              width: "string"
          },
      aggregations: { 
          _oTree : {type:'sap.ui.commons.Tree', multiple:false }
      }
      },
  
      init : function ( ){
          sap.ui.core.UIComponent.prototype.init.apply(this, arguments );
          var oSearchSettings = new sap.ui.model.json.JSONModel({
              "partially_search": "true",
              "case_sensitive": "true",
              "searchedText": "",
              "aSearchResults" : [],
              "indexOfSearchResults": "0"
          });
          sap.ui.getCore().setModel("settings", oSearchSettings);
          this.set_oTree(new sap.ui.commons.Tree());
      },
  
  
  
      createContent: function(){
          var oResponsiveLayout = new sap.ui.layout.form.ResponsiveGridLayout({columnsL: 3,columnsM: 2, columnS: 1});
          var oFormLayout = new sap.ui.layout.form.Form({
              width: this.getProperty("width"),
              height: this.getProperty("height"),
              layout: oResponsiveLayout,
              formContainers: [ new sap.ui.layout.form.FormContainer({
                                  formElements: [new sap.ui.layout.form.FormElement({
                                                     fields: [ this.oSearchField =  new sap.ui.commons.SearchField({
                                                      placeholder : "Suchtext / ID",
                                                      search: this._onSearchRequest,
                                                      layoutData: new sap.ui.layout.GridData({span: "XL8 L8 M8 S8"})
                                                  }), this.oArrowUp = new sap.ui.commons.Button({
                                                      width: "40px",
                                                      icon: "resources/images/arrow_up.png",
                                                      layoutData: new sap.ui.layout.GridData({span: "XL1 L1 M1 S1"})
                                                  }), this.oArrowDown = new sap.ui.commons.Button({
                                                      width: "40px",
                                                      icon: "resources/images/arrow_down.png",
                                                      layoutData: new sap.ui.layout.GridData({span: "XL1 L1 M1 S1"})
                                                  }),
                                                  this.oSettingsMenuButton = new sap.ui.commons.MenuButton({
                                                      height: "80%",
                                                      width: "50px",
                                                      icon: "resources/images/settings.png", 
                                                      lite: true,
                                                      layoutData: new sap.ui.layout.GridData({span: "XL2 L2 M2 S2"}),
                                                      menu: new sap.ui.unified.Menu({
                                                          items: [ new sap.ui.unified.MenuItem({
                                                              text: "ID",
                                                              submenu: new sap.ui.unified.Menu({
                                                                  items: [ this.oIdMenuButton = new sap.ui.unified.MenuItem({
                                                                      text: "IDs anzeigen/ausblenden",
                                                                      icon: "resources/images/check.png",
                                                                      select: this._onShowHideIdRequest
                                                                  })]
                                                              })
                                                          }), new sap.ui.unified.MenuItem({
                                                              text: "Suche",
                                                              submenu: new sap.ui.unified.Menu({
                                                                  items: [ new sap.ui.unified.MenuItem({
                                                                      text: "Teilsuche aktivieren/deaktivieren",
                                                                      icon: "resources/images/check.png",
                                                                      select: this._onPartiallySearchRequest
                                                                  }), new sap.ui.unified.MenuItem({
                                                                      text: "Groß- und Kleinschreibung beachten/ignorieren",
                                                                      icon: "resources/images/check.png",
                                                                      select: this._onCaseSensitiveRequest
                                                                  })]
                                                              })
                                                          })] 
                                                      })
                                                  }) ]
                                                 }),new sap.ui.layout.form.FormElement({
                                                     fields: [ this.get_oTree()]
                                                 })]
                              })]
          });
          return oFormLayout;
      },
  
      _bindData : function(){
          var oJsonModel = new sap.ui.model.json.JSONModel(this.getProperty("data"));
          this.getAggregation('_oTree').setModel(oJsonModel,"org_structJSON");
          this._loadNodes();
      },
  
      _loadNodes: function(){
          var rootPath = this._getRootPath();
          var aParentID = [];
          for(key in rootPath){
              var parentID = rootPath[key].parent;
              if(aParentID.indexOf(parentID) &lt; 0){
                  aParentID.push(parentID);
                  this._loadChildNodes(parentID);
              }
  
          }
      },
  
      _getRootPath : function() {
          var oJSON = this.oTree.getModel("org_structJSON");
          return oJSON.getProperty("/ORG_UNITS");
      },
  
      _loadChildNodes : function(parentID) {
          var rootPath = this._getRootPath();
          if(parentID === ""){
              for(key in rootPath){
                  var node = rootPath[key];
                  if(node.parent === parentID){
                      var rootNode = this._createNode(node.id, node.short_text);
                      this.getAggregation(_oTree).addNode(rootNode);
                  }
              }
          }else{
              var aNodes = this.oTree.findAggregatedObjects(true);
              for(var index = 0; index &lt; aNodes.length; index++){
                  var nodeID = aNodes[index].getId();
                  if(nodeID.slice(1,nodeID.length) === parentID){
                      for(key in rootPath){
                          if(rootPath[key].parent === parentID){
                              var oChildNode = this._createNode(rootPath[key].id, rootPath[key].short_text);
                              aNodes[index].addNode(oChildNode);
                          }
                      }
                  }
              }
          }
      },
  
      _createNode : function(id, short_text) {
          var oNode = new sap.ui.commons.TreeNode({
              id : "_" + id,
              icon : "resources/images/organisation_unit.gif",
              expanded: false
          });
          if(window.showId){
              oNode.setText(short_text + " (" + id + ")");
          }else{
              oNode.setText(short_text)
          }
          return oNode;
      },
  
      _onSearchRequest : function(oControlEvent) {
  
      },
  
      _onShowHideIdRequest : function(){
  
      },
  
      _onPartiallySearchRequest : function(){
  
      },
  
      _onCaseSensitiveRequest : function(){
  
      }
  
  
  });
  
  
  ui5_i.myComponent.Component.prototype.setTreeTitle = function(sTitle) {
      if(typeof(sTitle) === "string" &amp;&amp; sTitle != undefined &amp;&amp; sTitle !=null){
           this.setProperty("treeTitle", sTitle);
           this.get_oTree().setTitle(sTitle);
           return this;
      }else{
          throw("Property 'treeTitle' is not of type 'string' or 'undefined'!");
      }
  },
  
  ui5_i.myComponent.Component.prototype.setData = function(oData) {
      if(typeof(oData) === "object" &amp;&amp; oData != undefined &amp;&amp; oData !=null){
              this.setProperty("data", oData);
              this._bindData();
              return this;
      }else{
          throw("Property 'data' is not of type 'object' or undefined!");
      }
  },
  
  ui5_i.myComponent.Component.prototype.setSearchPlaceholder = function(sText) {
      if(typeof(sText) === "string" &amp;&amp; sText != undefined &amp;&amp; sText !=null){
          this.setProperty("searchPlaceholder", sText);
          this.oSearchField.setPlaceholder(sText);
          return this;
      }else{
          throw("Property 'searchPlaceholder' is not of type 'string' or 'undefined!'");
      }
  }
  </code></pre>