Could someone troubleshoot this actionscript 3.0 code?

narduell
narduell used Ask the Experts™
on
Could someone tell me why I'm getting this error message?

TypeError: Error #2007: Parameter listener must be non-null.
      at flash.events::EventDispatcher/addEventListener()
      at flash.display::Stage/addEventListener()
      at main_fla::MainTimeline/frame20()

From this code:

import flash.display.MovieClip;
      import flash.text.TextField;
      import flash.text.StyleSheet;
      import flash.events.Event;
      import flash.net.URLRequest;
      import flash.net.URLLoader;
      

            var _html:String;
            var _css:StyleSheet;
            
            
            function Main()
            {
                  init();
            }
            
            function init():void
            {
                  stage.frameRate=31;
                  
                  loadHTML();
            }
            
            function loadHTML():void
            {
                  var loader:URLLoader=new URLLoader();
                  var request:URLRequest=new URLRequest('text/info.html');
                  loader.addEventListener(Event.COMPLETE,onHTML);
                  loader.load(request);
            }
            
            function onHTML(evt:Event):void
            {
                  _html=evt.target.data;
                  
                  loadCSS();
            }
            
            function loadCSS():void
            {
                  var loader:URLLoader=new URLLoader();
                  var request:URLRequest=new URLRequest('styles/style_alt.css');
                  loader.addEventListener(Event.COMPLETE,onCSS);
                  loader.load(request);
            }
            
            function onCSS(evt:Event):void
            {
                  _css=new StyleSheet();
                  _css.parseCSS(evt.target.data);
                  external_txt.styleSheet=_css;
                  external_txt.wordWrap=true;
                  external_txt.htmlText=_html;
                  external_txt.condenseWhite=true;

            }
            
      stage.addEventListener(Event.ENTER_FRAME, Main());


Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
This would be the first line that I noticed...  You do not put () in this line...
   stage.addEventListener(Event.ENTER_FRAME, Main);

and this line...
   function Main()
should be this because of the previous change...
   function Main(e:Event)

CyanBlue
Commented:
CyanBlue is correct about why you're getting that error message, and how to fix it.  but beyond that, i don't think you want to run that code in an enterframe.  that'd load 62 files every second, that does exactly the same thing every time.  i suspect you just want to take that last line out.

if so, remove this:

stage.addEventListener(Event.ENTER_FRAME, Main());

Author

Commented:
Thank you both!

I did try CyanBlue's solution before posting and got nothing in the text box, the code was broken. I do get text in the box with the code I posted above but the error message too!

Could you tell me what to use to call the function if not enter-frame? I'm using SWAddress and want the text to load when the user clicks on the navigation and comes to the frame.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
An update - I tried the suggestions CyanBlue made and it did work this time! I must have done something wrong when I tried it on my own.

But I still would like to call the function with a better event.

Thx.

Commented:
any functions in your enterFrame handler will fire 31 times every second.  that means the css file and the html file are going to be loaded 31 times every second - that's going to be a tremendous load on the application and the server.

you don't need it in an event at all - that class was intended for use a document class.  someone else had that same code posted a week or two ago they needed modifications to.

Author

Commented:
I agree. It was a document class and I tried to implement the solution that was posted but I could not have text loaded on different frames using the array because it was looking for all the text mc's to be on the same frame when i tried it. Also, the second text box lost its formatting for some strange reason. The CSS wasn't loading for the second text box I think. So my amateur solution was to write it into the fla file which I know is not optimum and then I needed a way to call it. I'm sure there is a better way and would be thrilled if you could suggest one.

Thanks very much.

Commented:
my first suggestion would be to have all the textfields available immediately, populate them with text, load the one style sheet, apply that style sheet to each, then hide/show them as appropriate (using the visible property, or addChild/removeChild).  if that isn't feasible, you could just call the load functions in the frames where the specific textfields exist.

Author

Commented:
When I tried to use Main.as as a document class I got a lot of errors I'm guessing SWFAddress and Main were not playing nicely together though I don't know that for sure. I tested the document class method with an array in a clean fla just to see if it was worth it to pursue. When I got the issue with losing the CSS on the second text box and the problem of having to load all the text at once then I thought to try a different way so didn't continue troubleshooting Main with SWFAddress.

 I wonder if I could turn the text fields into movieclips then link them to a Main class and pass the text file I need to access as a variable. Problem is I don't know how to change the code so I can pass the variable. Do you think the idea would work? Is it worth trying to figure out?

Can I just use another event to call the function that isn't so load heavy?

Commented:
i'm still not 100% clear on what you're trying to accomplish.  might be best to upload your project files with a very specific, step-by-step explanation of the net result you want.

Author

Commented:
Sure. Attached are the project files. I had been using a way to load external text from a .txt file and styling it with CSS (see commented out actionscript on frame 20. However, I was running into problems using bold fonts with that method. Just couldn't make them happen. So I thought I would look around for another way to do this. I found the Main.as example and the bold worked. So have been trying to use that. It wouldn't work for me with the SWFAddress code I have in my .fla (perhaps easily solved but I'm not very expert at actionscript so it looked daunting to me when I got a lot of error messages). So then I thought I would use the code right in the .fla which works. However, I understand your point about the enter_frame event so I guess I should keep looking for a better way.
I should say that right now there is only one text box in my .fla but ultimately I will have many so need to solve this as smartly as possible. Writing the code into the .fla is not very swift but it was the only thing I was able to do with my skill level.

Sorry this is slow! I wasn't able to upload archived folders because they had invisible files in them so I will have to upload them one by one! They are all in the same folder. Not uploading images or pdfs so you will get an error message on those. swfaddress.js should reside in a folder called swfaddress.
And expressinstall.swf and swfobject.js should reside in a folder called swfobject.

Thanks, sorry for the mess below. Can't figure out another way to do this.
c.fla
c.swf
index.html
main.fla
styles.zip
swfaddress.js
SWFAddress.as
SWFAddressEvent.as
expressinstall.swf
expressinstall.swf
text.zip

Commented:
no problem - i've got to go for the night, but will download everything and take a look tomorrow after work

Author

Commented:
Thanks so much!

Commented:
ok i think i see what you want to do - you just want to load different text on different frames, right?  and the textfields you want to load the text into don't exist immediately...  is that correct?

if so, and without suggesting an approach that would require a very drastic redesign of what you have already, i'd use plain ol' timeline functions.

in the first frame (or somewhere in the first few frames), create a function that'll assign the contents of a text file to a text field.  something as simple as:

function assignText(file:String,target:TextField):void{
  var loader:URLLoader = new URLLoader();
  var request:URLRequest = new URLRequest(file);
  var onComplete:Function = function(event:Event):void{
    target.htmlText = event.target.data;
  }
  loader.load(request);
}

normally, not a huge fan of the nested handler, but like i said, trying to keep it simple and without a major rewrite.  anyways, you could then call that function in the appropriate frame, passing the appropriate file and target textfield:

assignText("text/info.html", infoTextfield);
assignText("text/bio.txt", bioTextField);

for the css, just load it once (also early), and assign it to a variable.  then as each textfield becomes available in later frames, just assign it.  basically, early on you'd say:

var styles:StyleSheet = new StyleSheet();

var cssLoader:URLLoader=new URLLoader();
var cssRequest:URLRequest=new URLRequest('styles/style_alt.css');
cssLoader:.addEventListener(Event.COMPLETE,onCSS);
cssLoader:.load(cssRequest);

function onCss(event:Event):void{
  styles.parseCSS(event.target.data);
}

then in later frames, instead of reloading it over and over, just assign it:

someTextField.styleSheet = styles;

you could do that call and the assign text function call in the same frame.

hope that helps

Author

Commented:
Very helpful. I think it is almost there. I've attached the fla again because I just can't get the text to appear though I did get rid of all the code glitches, no erros now. Can you see what I'm doing wrong from the file?
main.fla

Commented:
oops - my fault.  i forgot to add the onComplete as a listener.  change assignText to this:
function assignText(file:String,target:TextField):void{
  var loader:URLLoader = new URLLoader();
  var request:URLRequest = new URLRequest(file);
  var onComplete:Function = function(event:Event):void{
    target.htmlText = event.target.data;
  }
  loader.addEventListener("complete", onComplete);
  loader.load(request);
}

Open in new window

Author

Commented:
That did the trick! Thanks so much for helping.

Commented:
glad you got it working

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial