Question

If I execute the code below in FlashBuilder, I get the following error (I translate it)

TypeError: Error #1009: Access to an Attribute or Method of an null-Object is not possible.
 at components::NumDisplay()[\src\components\NumDisplay.mxml:39]

This line in NumDisplay.mxml is the problem:

[Bindable]
public var oneled_top:OneDisplay = new OneDisplay(numberData.led_top);

If i change it from the above to:

[Bindable]
public var oneled_top:OneDisplay = new OneDisplay(1);

It is working, because I send a real Number. So how can I access the value from numberData.led_top?

If I test the access in the samefile NumDisplay.mxml with the line

<s:Label text="{numberData.led_top}" color="#FF0000">
</s:Label>

it accesses the value, the same as if I put it in my component

<components:oneLedDisplay showData="{numberData.led_top}" x="10" y="10" />

I don't get it after searching a couple of hours... Thanks in advance.

My main method tasachenrechner.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="600" minHeight="500" xmlns:components="components.*">
 <fx:Script>
            <![CDATA[
   import components.NumberDisplay;
   [Bindable]
   protected var firstNumber:NumberDisplay = new NumberDisplay(1);
   [Bindable]
   protected var secondNumber:NumberDisplay = new NumberDisplay(2);
             ]]>
 </fx:Script>

 <components:NumDisplay
  numberData="{firstNumber}"
  x="10" 
  y="20"/>

 <components:NumDisplay 
  numberData="{secondNumber}"
  x="73" 
  y="20"/>

</s:Application>

My AS-Class NumberDisplay.as:

package components
{
 import flash.display.DisplayObject;

 [Bindable]
 public class NumberDisplay
 {
  public var num:Number;

  public var led_top:Number=0;
  public var led_r1:Number=0;
  public var led_r2:Number=0;
  public var led_middle:Number=0;
  public var led_l1:Number=0;
  public var led_l2:Number=0;
  public var led_bottom:Number=0;

  public function NumberDisplay(num:Number)
  {
   this.num = num;
   switch(this.num)
   {
    case 0:
     trace("ZERo");
     break;
    case 1:
     led_top = 1;
     led_r1 = 1;
     led_r2 = 1
     trace("EINS" + led_top + " num:" + num);
     break;
                            //[... some more cases]
    default:
     break;
   }
  }
 }
}

My NumDisplay.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
   xmlns:s="library://ns.adobe.com/flex/spark" 
   xmlns:mx="library://ns.adobe.com/flex/mx" width="45" height="59"
   xmlns:components="components.*">

 <fx:Style>
  @namespace s "library://ns.adobe.com/flex/spark";
  @namespace mx "library://ns.adobe.com/flex/mx";
  @namespace components "components.*";
 </fx:Style>

 <fx:Script>
  <![CDATA[
   import components.NumberDisplay;
   import components.OneDisplay;

   [Bindable]
   public var numberData:NumberDisplay;

   [Bindable]
   public var oneled_top:OneDisplay = new OneDisplay(numberData.led_top);
                        // some more init calls of data-objects same type
  ]]>
 </fx:Script>


 <s:Label text="{numberData.led_top}" color="#FF0000">
 </s:Label>

 <components:oneLedDisplay showData="{oneled_top}" x="10" y="10" />
        // some more objects of same type
</s:Group>

My AS-Class OneDisplay.as:

package components
{
 import flash.display.DisplayObject;

 public class OneDisplay
 {
  [Bindable]
  public var show:Number;
  [Bindable]
  public var value:Number=0;

  public function OneDisplay(show:Number)
  {
   this.show = show;
   switch(this.show) 
   {
    case 0:
     value = 0.3;
     trace(value);
     break;
    case 1:
     value = 1.0;
     trace(value);
     break;
   }
  }
 }
}

My oneLedDisplay.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
   xmlns:s="library://ns.adobe.com/flex/spark" 
   xmlns:mx="library://ns.adobe.com/flex/mx">
 <fx:Script>
  <![CDATA[
   import components.OneDisplay;
   [Bindable]
   public var showData:OneDisplay;
  ]]>
 </fx:Script>

 <s:Rect id="stroke" width="40" height="6" alpha="{showData.value}">
  <s:fill>
   <s:SolidColor color="#000000"/>
  </s:fill>
 </s:Rect>

 <s:Label text="{showData.value}" color="#FF0000">
 </s:Label>

</s:Group>
Was it helpful?

Solution

Remember that you are not only assigning a value, but declaring the member variable oneled_top. At that point, you cannot access numberData because it has not been instantiated (there is no call to new NumberData()! You have to find a way to make your call to new OneDisplay (numberData.led_top) at a later time, when there actually is a value to access.

OTHER TIPS

You provided lots of code which I don't want to reverse engineer.

The answer is that oneled_top is being initialized before numberData. You have no control over initialization of variables when using MXML.

Set default values in the commitProperties() method, or if oneled_Top is supposed to be a skin part, set the default values in the PartAdded method.

You'll benefit from reading up on the Component Lifecycle.

You can use BindingUtils.bindSetter() to detect changes to numberData and then initialize oneled_top

BindingUtils.bindSetter(_setOneLabel_top, this, "numberData");

and setter:

function _setOneLabel_top(disp:NumberDisplay):void
{
  /* if(this.oneled_top == null) */
  this.oneled_top = new OneDisplay(disp.led_top);
}

But I think, that u're using [Bindable] where you shouldn't have to need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top