Jump to content
Search Community

Cant access a null object that isnt null

Waskom test
Moderator Tag

Recommended Posts

Hello all, 

 

Just recently got TweenLite/Max and am still new to it. So this may be an oversight on my part. 

 

I am creating a movie clip and using it as a button. When the button is clicked it creates a box(movie clip), inside the box is a series of bars(each one a movie clip). The box is then tween down in a simple animation. If the button is clicked again, it raises the box. If the button is clicked again it lowers the box. 

 

There is a check when the button is clicked to see if the box has already been created. If it has, then it skips that part and either raises or lowers it based on its current state.

 

The button creates fine, it will create the box and bars, and lower it all just fine. But when i click the button again, to raise the box, i get: 

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.greensock::TweenLite/render()
at com.greensock.core::SimpleTimeline/render()
at com.greensock.core::Animation$/_updateRoot()
 
It will continuously create this error as if it stuck in a loop. I ran the debugger and got:
 

Error: Cannot tween a null object. Duration: 1, data: undefined
at com.greensock::TweenLite()[C:\...\com\greensock\TweenLite.as:448]
at com.greensock::TweenLite$/to()[C:\...\com\greensock\TweenLite.as:910]
at TrainingModulev2_fla::MainTimeline/Index_Drop()[TrainingModulev2_fla.MainTimeline::frame1:146]

 

Frame1:146 is the 4th from last line in my code below.(  TweenLite.to(IndexBox, 1, {delay:0, y:58});  )

 

My code:

function Index_Button()
{
  var IndexButton:MC_IndexButton = new MC_IndexButton  ;
  addChild(IndexButton);
  IndexButton.x = -50;
  IndexButton.y = 110;
  IndexButton.alpha = 0;
  TweenLite.to(IndexButton, 1, {x:1, alpha:1});
  IndexButton.addEventListener(MouseEvent.CLICK, Index_Drop);
}

function Index_Drop(e:MouseEvent)
{
  if (IndexDown==true)
    {
	TweenLite.to(IndexBox, 1, {y:"-58"});
	IndexDown = false;
    }
    else if (IndexCreated == false)
    {
	var IndexBox:MC_IndexBox = new MC_IndexBox  ;
	addChild(IndexBox);
	IndexBox.x = 0;
	IndexBox.y = -184.5;
	var IndexBarArray:Array = [];
	for (i=1; i<8; i++)
	{
    	  IndexBarArray[i] = new MC_IndexBar  ;
	  IndexBarArray[i].name = "IndexBar" + i;
	  IndexBox.addChild(IndexBarArray[i]);
	  IndexBarArray[i].x = 14;
	  IndexBarArray[i].y = i * 27 + 72;
	  IndexBarArray[i].addEventListener(MouseEvent.MOUSE_OVER, IndexBar_Over);
	  IndexBarArray[i].addEventListener(MouseEvent.MOUSE_OUT, IndexBar_Out);
	}
        setChildIndex(IndexBox, 2);
        IndexCreated = true;
        TweenLite.to(IndexBox, 1, {delay:0, y:58});
  }
  else
    {
	TweenLite.to(IndexBox, 1, {delay:0, y:58});
	IndexDown = true;
    }
}

I cant figure out what im doin wrong. From what i am understanding, That line isnt ran until after IndexBox is created. I know its there, I've targeted it a few times in the code. I can see it when i run a test. 

 

thanks

Link to comment
Share on other sites

Hi and welcome to the forums.

 

Seems like a scope issue.

 

Even though IndexBox is created, it doesn't mean it is accessible from everywhere.

 

add just this simple trace BEFORE your tween

trace("IndexBox is " + IndexBox);
TweenLite.to(IndexBox, 1, {delay:0, y:58});
IndexDown = true;

I'm guessing you are going to get undefined

 

---

 

The main problem is that your IndexBox var is declared locally inside the IndexDown function only when certain conditions exist. By the time that function runs agains and certain conditions are met that make the problematic tween run... the value of IndexBox has no meaning or its in a scope totally separate from the tween code.

 

Here is basic example of what is happening

 

BAD since myName is declared inside run() it is inaccessible by code outside that function

function run(){
   var myName = "carl";   
}
run();
trace(myName);
GOOD myName is declared outside the run function and is now globally accessible
 
var myName:String;
function run(){
  myName = "carl";   
}
run();
trace(myName);
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...