views:

109

answers:

1

My animation is advanced forward, but it's frozen. It throws a TypeError 1009. How do I get rid of this error and get it to play?

download
http://sandboxfun.weebly.com/

XML

<?xml version="1.0" encoding="utf-8"?>
<SESSION>
 <TIMER TITLE="speed">1000</TIMER>
 <COUNT TITLE="starting position">10000</COUNT>
</SESSION>

FLA

//DynamicText 'Count'
 var timer:Timer = new Timer(10);  
 var count:int = 0;
 var fcount:int = 0; 

timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
 timer.start();  

function incrementCounter(event:TimerEvent) { 
 count = myXML.COUNT.text();
 count++;  
 fcount=int(count*count/1000);
 mytext.text = formatCount(fcount);
 }

function formatCount(i:int):String { 
 var fraction:int = i % 100; 
 var whole:int = i / 100;  

 return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction); 
 } 

//LOAD XML
 var myXML:XML;
 var myLoader:URLLoader = new URLLoader();
 myLoader.load(new URLRequest("time.xml"));
 myLoader.addEventListener(Event.COMPLETE, processXML);
  /*------CHANGED TIMER VALUE WITH XML------*/
  timer = new Timer( Number(myXML.TIMER.text()) );
  //timer.start();

//PARSE XML
function processXML(e:Event):void {
 myXML = new XML(e.target.data);
  trace(myXML.COUNT.text()); 
  trace(myXML.TIMER.text()); 
 }
//var count:int = 0;//give it a value type
 /*------CHANGED COUNT VALUE WITH XML------*/
 count = myXML.COUNT.text();

ERROR

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at _fla::MainTimeline/frame1()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at _fla::MainTimeline/incrementCounter()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

+1  A: 

You're making the same very simple mistake you were making in one of your previous questions, and it shows that you don't yet have a very firm grasp of the way your code is being executed, in what order, etc. I've been through it and got rid of the errors, but the code doesn't do anything except display some numbers. I don't know if that's the goal though.

Before I paste the code, a few notes on yours. For one, it is once again horribly messy. I've formatted it properly, but I won't be doing that again with your code. It's the easiest thing in the world to keep code formatted well, and it really makes other people's lives easier when trying to interpret it. I really, strongly suggest you actually start doing this.

Secondly, the problem with your code, and the errors you were getting, were as a result of trying to access the contents of variables which you hadn't yet set. The main culprit was your myXML object, which you were trying to access within incrementCounter before you had even run the processXML function. This is fairly basic, and you should learn how code executes in order to avoid similar problems. Also, I would avoid putting variable declarations in between functions if you're coding straight onto the timeline, it can and will get horribly confusing. Put all declarations at the top, then call some function which will get things going, and have your functions below. I've rearranged your code to reflect this advice.

Finally, these types of errors (the 'cannot access a property or method of a null' etc) can usually be very easily diagnosed using Flash's limited debugger. Run 'Debug Movie' from the Debug menu in Flash. The advantage of the debugger is that it will not only show you the error in the Output window, but it will stop at the line of code that caused the error, so you can see what the problem is. There's even a window which displays all variables in the current scope, so you can see which one's null, or whatever. Using this you'd have been able to fix these errors yourself; TypeErrors like these aren't generally suitable for posting on a forum. Of course if you're really stuck, you should ask, but it's good to learn how you can fix these things on your own, and many of your posts are repeating the same questions.

Anyway, that modified code:

//DynamicText 'Count'
var timer:Timer = new Timer(10);  
var count:int = 0;
var fcount:int = 0; 

//LOAD XML
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("time.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
/*------CHANGED TIMER VALUE WITH XML------*/
//timer.start();

function incrementCounter(event:TimerEvent) 
{ 
    //Before, this function was being called before 
    //you had read in your XML file, so myXML was empty.
    count = myXML.COUNT.text();
    count++;  
    fcount=int(count*count/1000);
    mytext.text = formatCount(fcount);
}

function formatCount(i:int):String 
{ 
    var fraction:int = i % 100; 
    var whole:int = i / 100;  

    return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction); 
} 


//PARSE XML
function processXML(e:Event):void 
{
    myXML = new XML(e.target.data);
    trace(myXML.COUNT.text()); 
    trace(myXML.TIMER.text()); 
    count = myXML.COUNT.text();

    //I put this here, so that this timer's event will only be 
    //created once myXML is initialised with XML data.
    timer = new Timer( Number(myXML.TIMER.text()) );
    timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
    timer.start();  
}
debu
Thanks. Now I've got to figure out why it doesn't animate. I don't know if the values are set statically or it's the way the timer events unfold. I'll try to get away from debug posts, but I didn't thing it was messy:)
VideoDnd
Didn't think it was messy huh? Well it was, no two ways about it!
debu