Action Script all in frame 1, but errors persist

Dear Readers:

I have the following code all in frame one of the timeline on an actions layer.  I set some if statements so that certain code wouldn't be executed until the timeline reached a specific frame, so as to avoid "null object references".  However, I am still getting them.  

Can anyone help me out?


I have included line numbers for the code

The interactivity is as such:  I log in as "Janet Taylor" with a password in frame 1 and it is supposed to take me to frame 2 where I can access the logged-in state interface options such as clicking on buttons to activate features (like search and clicking on "View Contacts").
TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at 11MockupsAll_ha_interactive101trace_fla::MainTimeline/frame1()[11MockupsAll_ha_interactive101trace_fla.MainTimeline::frame1:30]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at 11MockupsAll_ha_interactive101trace_fla::MainTimeline/clickLogIn()[11MockupsAll_ha_interactive101trace_fla.MainTimeline::frame1:22]
code: (all on frame 1 spanning the timeline)
1  stop();
2  var uName:String = userName.text;
3  var searchMC:search_mc = new search_mc();
4  var addMC:add_mc = new add_mc();
5  var addMeMC = new function();
7  btnLogIn.addEventListener(MouseEvent.CLICK,clickLogIn);
8  function clickLogIn(evt:MouseEvent):void{
9  	if(currentFrame==1){
10  	if(userName.text=="Janet Taylor" && passWord.text=="teacher"){
11		uName=userName.text;
12		logName.text = uName;
13		gotoAndStop("homepage");
16	}
17	}
18  }
20  btnLogIn1.addEventListener(MouseEvent.CLICK,clickLogIn1);
21  function clickLogIn1(evt:MouseEvent):void{
22	if(currentFrame==14){
23	if(userName1.text=="Janet Taylor" && passWord1.text=="teacher"){
24		uName=userName1.text;
25		gotoAndStop("regisThanks");
26		logName1.text=uName;		
27	}
28	}
29  }
31  btnLogOut.addEventListener(MouseEvent.CLICK,clickLogOut);
32  function clickLogOut(evt:MouseEvent):void{
33	if(currentFrame!=1){
34		gotoAndStop("regisThanks");
35	}
36  }
37  btnLogOut1.addEventListener(MouseEvent.CLICK,clickLogOut1);
38  function clickLogOut1(evt:MouseEvent):void{
39	if(currentFrame!=13||currentFrame!=14){
40		gotoAndStop("regisThanks");
41	}
42  }
46  btnRegister.addEventListener(MouseEvent.CLICK,clickRegister);
47  function clickRegister(evt:MouseEvent):void{
48	if(currentFrame==1){
49		gotoAndStop("register");
50	}
51  }
52  btnRegister1.addEventListener(MouseEvent.CLICK,clickRegister1);
53  function clickRegister1(evt:MouseEvent):void{
54	if(currentFrame==13||currentFrame==14){
55		gotoAndStop("register");
56	}
57  }
60  btnGo.addEventListener(MouseEvent.CLICK,clickGo);
61  function clickGo(evt:MouseEvent){
62	addChild(searchMC);
63	searchMC.x=200;
64	searchMC.y=200;
65	searchMC.dynText.text=inpSearch.text;
66  }
68  addMeMC=addMC;
69  searchMC.closeBox1.addEventListener(MouseEvent.CLICK,closeSearch);
70  function closeSearch(evt:MouseEvent){
71	removeChild(searchMC);
72  }
73  addMC.closeBox2.addEventListener(MouseEvent.CLICK,closePopUp);
74  function closePopUp(evt:MouseEvent){
75	removeChild(addMC);
76  }
77  btnHOME_NavT.addEventListener(MouseEvent.CLICK,clickHOME);
78  function clickHOME(evt:MouseEvent){
79	gotoAndStop("homepage");
80  }
82  btnABOUT_NavT.addEventListener(MouseEvent.CLICK,clickABOUT);
83  function clickABOUT(evt:MouseEvent){
84	gotoAndStop("about");
85  }
87  btnHELP_NavT.addEventListener(MouseEvent.CLICK,clickHELP);
88  function clickHELP(evt:MouseEvent){
89	gotoAndStop("help1");
90  }
92  btnFAQ.addEventListener(MouseEvent.CLICK,clickFAQ);
93  function clickFAQ(evt:MouseEvent){
94	if(currentFrame==2){
95	gotoAndStop("help1");
96	}
97  }
99  btnAddContacts.addEventListener(MouseEvent.CLICK,clickAddContact);
100 function clickAddContact(evt:MouseEvent){
101	if(currentFrame==2){
102	addChild(addMC);
103	addMC.x=100;
104	addMC.y=300;
105	}
107 }
108 btnAddContactsNav.addEventListener(MouseEvent.CLICK,clickAddContactNav);
109 function clickAddContactNav(evt:MouseEvent){
111	addChild(addMC);
112	addMC.x=100;
113	addMC.y=300;
115 }
116 addMC.addContactPopUp.addEventListener(MouseEvent.CLICK,clickAddPopUp);
117 function clickAddPopUp(evt:MouseEvent){
118	gotoAndStop("addContact");
119 }
120 addMC.importPopUp.addEventListener(MouseEvent.CLICK,clickImportPopUp);
121 function clickImportPopUp(evt:MouseEvent){
122	gotoAndStop("importContact");
123 }
124 btnViewContacts.addEventListener(MouseEvent.CLICK,clickViewContact);
125 function clickViewContact(evt:MouseEvent){
126	if(currentFrame==2){
127	gotoAndStop("viewContacts");
128	}
129 }
130 btnViewContactsNav.addEventListener(MouseEvent.CLICK,clickViewContactNav);
131 function clickViewContactNav(evt:MouseEvent){
132	gotoAndStop("viewContacts");
133 }
135 btnDeleteContacts.addEventListener(MouseEvent.CLICK,clickDeleteContact);
136 function clickDeleteContact(evt:MouseEvent){
137	gotoAndStop("deleteContact");
138 }
139 btnDeleteContactsNav.addEventListener(MouseEvent.CLICK,clickDeleteContactNav);
140 function clickDeleteContactNav(evt:MouseEvent){
141	gotoAndStop("deleteContact");
142 }
143 /*
144 btnDeleteContactsNav1.addEventListener(MouseEvent.CLICK,clickDeleteContactNav1);
145 function clickDeleteContactNav1(evt:MouseEvent){
146 	gotoAndStop("deleteContact");
147 }*/
149 btnExportContacts.addEventListener(MouseEvent.CLICK,clickExport);
150 function clickExport(evt:MouseEvent){
151	gotoAndStop("exportContact");
152 }
153 btnParentLog.addEventListener(MouseEvent.CLICK,clickParentLog);
154 function clickParentLog(evt:MouseEvent){
155	gotoAndStop("parentLog");
156 }
158 btnExportContactsNav.addEventListener(MouseEvent.CLICK,clickExportNav);
159 function clickExportNav(evt:MouseEvent){
160	gotoAndStop("exportContact");
161 }
163 btnParentLogNav.addEventListener(MouseEvent.CLICK,clickParentLogNav);
164 function clickParentLogNav(evt:MouseEvent){
165	gotoAndStop("parentLog");
166 }
167 /*
168 btnExportContactsNav1.addEventListener(MouseEvent.CLICK,clickExportNav1);
169 function clickExportNav1(evt:MouseEvent){
170	gotoAndStop("exportContact");
171 }
173 btnParentLogNav1.addEventListener(MouseEvent.CLICK,clickParentLogNav1);
174 function clickParentLogNav1(evt:MouseEvent){
175	gotoAndStop("parentLog");
176 }
177 */
178 /*
179 btnLogOut.addEventListener(MouseEvent.CLICK,clickLogOut);
180 function clickLogOut(evt:MouseEvent){
181	gotoAndStop("regisThanks");
182 }
183 */
185 //logName.text=userName.text;
187 //trace(logName.text);
188 //stop();
190 //import fl.controls.Button;
191 addMC=addMeMC;
192 logName1.text=uName;
194 searchMC.closeBox1.addEventListener(MouseEvent.CLICK,closeSearch1);
195 function closeSearch1(evt:MouseEvent){
196	removeChild(searchMC);
197 }
199 addMC.closeBox2.addEventListener(MouseEvent.CLICK,closePopUp1);
200 function closePopUp1(evt:MouseEvent){
201	removeChild(addMC);
202 }
205 btnHOME_NavT.addEventListener(MouseEvent.CLICK,clickHOME1);
206 function clickHOME1(evt:MouseEvent){
207	gotoAndStop("homepage");
208 }
210 btnABOUT_NavT.addEventListener(MouseEvent.CLICK,clickABOUT1);
211 function clickABOUT1(evt:MouseEvent){
212	gotoAndStop("about");
213 }
215 btnHELP_NavT.addEventListener(MouseEvent.CLICK,clickHELP1);
216 function clickHELP1(evt:MouseEvent){
217	gotoAndStop("help1");
218 }
219 btnAddContactsNav1.addEventListener(MouseEvent.CLICK,clickAddContactNav1);
220 function clickAddContactNav1(evt:MouseEvent){
221	//removeChild(addMC);
222	addChild(addMC);
223	addMC.x=100;
224	addMC.y=300;
225 //	gotoAndStop("addContact");
226 }
227 addMC.addContactPopUp1.addEventListener(MouseEvent.CLICK,clickAddPopUp1);
228 function clickAddPopUp1(evt:MouseEvent){
229	gotoAndStop("addContact");
230 }
231 addMC.importPopUp.addEventListener(MouseEvent.CLICK,clickImportPopUp1);
232 function clickImportPopUp1(evt:MouseEvent){
233	gotoAndStop("importContact");
234 }
235 btnViewContactsNav1.addEventListener(MouseEvent.CLICK,clickViewContactNav1);
236 function clickViewContactNav1(evt:MouseEvent){
237	gotoAndStop("viewContacts");
238 }
240 btnDeleteContactsNav1.addEventListener(MouseEvent.CLICK,clickDeleteContactNav1);
241 function clickDeleteContactNav1(evt:MouseEvent):void{
242	gotoAndStop("deleteContact");
243 }
244 btnExportContactsNav1.addEventListener(MouseEvent.CLICK,clickExportNav1);
245 function clickExportNav1(evt:MouseEvent){
246	gotoAndStop("exportContact");
247 }
249 btnParentLogNav1.addEventListener(MouseEvent.CLICK,clickParentLogNav1);
250 function clickParentLogNav1(evt:MouseEvent){
251	gotoAndStop("parentLog");
252 }

Open in new window

Who is Participating?
blue-genieConnect With a Mentor Commented:
where you need to take it from this point in debug.
as i said, where is logName1.text?

if that exists somewhere else on the frame - then i repeat that is your problem.
i only looked at your first frame as that is where your code is and that is where the problem is.

what do you mean by duplicate name references that appear and disappear.
You should be using addChild() and removeChild for that.

my opinion, you're trying to stick to what you know it setting up a file with AS2 but you want to use AS3 - you need to start implementing the AS3 way -have a look in the help file about the DisplayList and how to add objects to your stage.

you need to do some work if you want to fix your problem.
start with logName1.text=uName;

Hi mate,

I'm guessing that the instances of the clips you're referring to in frame one don't exist on frame on of the movie? I would tend to do this the opposite way round. Write functions on frame 1 and call them by name from the within the navigation buttons.

In addition, many of your functions are fairly similar and could accept params from the buttons when they are called. Does that make sense?
markerlineAuthor Commented:
I'm not sure, court_3, what the process is for calling functions on buttons.  This would be very helpful, if I understand the concept.  I think in AS 2.0 you could attach code to Movie Clips and make them interactive, right?  Well, I tried that in AS 3.0 but I must have gone about it the wrong way so it didn't work for me.  

If you have the solution at hand, I would love to have you share it so I can test it out.

Thanks in advance for your help and thank you period for responding to my question (with such a long code snippet).

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Hi Markerline,

Yes, the old practice of attaching actions directly to movieclips has been deprecated - and rightly so!

What I'm talking about is your movie clip buttons having their own 1 frame time line (as I'm sure they already do) but also having an actions layer. The "embedded" timeline can contain your listener for the click within the clip itself. Therefore using something like...


...would call the function in the parent timeline

How does that sound?

markerlineAuthor Commented:
Wow.  How simple that sounds.  [Aside:  I will try that right after I watch the weather forecast.  I want to make sure I don't get caught in the rain this weekend.]

Thanks a mil.  I'll let you know how it works.

markerlineAuthor Commented:
Dear court_3:

I tried replacing the scripts and placing them into a Button component.  Regular Buttons cannot have actions attached to them.  But I get a compiler error which says that the component already has class definitions and I need to override the class definitions with my own if I want to use actions.  Uh oh.  I don't know how to write classes.

Hi mate,

What are your buttons made of then? Are they not just regular movieclips?
where are all you button items in terms of the frames?
markerlineAuthor Commented:
Dear court_3 and blue-genie:  My buttons are scattered across frames.  At first all of my buttons were comprised of button objects.  But the only way to use scripts on buttons would be to embed them in movie clips, which I did not do, or to use the Button Component, which I tried for only two instances at a single frame occurrence.  I guess I thought that if I were to use movie clips and attach actions to them that would not work.  I will try to convert the buttons to movie clips and attach scripts to those frames and see what happens.

Hey Markerline, that's what I was recommending in my first post. I think your errors are related to the fact that you are referencing instances that do not exist in frame one of the timeline. If you make your buttons MovieClips with their own timeline and actions layer, you will be able to call root functions from them and, presumably, without any errors!

Let us know how you go!
markerlineAuthor Commented:
Well.  Now I tried to call them as Movie Clips as you suggested but I'm getting the following compiler errors for the second button MC:

The error below also happens for the instance of the first button MC with the name of the property replaced by the name that should be assigned to that button (clickLogIn).  I think I'm not defining "btnLogIn" in the button MC.  Should I define that as a var first like "var btnLogin:Object new btnLogin();" ?
1119: Access of possibly undefined property clickRegister through a reference with static type flash.display:DisplayObjectContainer.

Open in new window

I'm gonna try to replicate the problem this end so give me a while!
markerlineAuthor Commented:
Thanks.  I hope you solve it.  You're a great contributor.
Hey, I haven't come up with the answer yet!!! ;-)
Replicated the exact same error mate and am now trying to sort. It's clearly a target path problem but I haven't got to the bottom of it... Yet!
markerlineAuthor Commented:
Thanks for the update court_3 . . .  

I'll be looking for a solution but take your time.  It's no rush.

i haven't looked at your stuff but if you guys don't come right.
i'd put my money on the fact that your code is all on the first frame and your buttons are scatter across frames - hence your code loads in the first frame and it can't find any of the buttons in later frames - thus it throws a null reference error.
Hi Blue Genie, that's definitely the problem. The question has evolved more into one of "how to call a root function from the timeline of a child movieclip". For somone of your level, I'm sure you can answer that in 2 seconds flat!
assuming you have a document class called Box

and then you have a SmallBox class which you then add to your stage.

var smBox:SmallBox = new SmallBox();

and you have a public function on your Box class

public function changeMyColor():void {

you can access that function from smBox using


markerlineAuthor Commented:
Blue-genie.  It looks like it makes sense but are there tutorials on writing classes?  Classes are not new but more in use in AS 3.0 and I never had to use them in AS 2.0 (Obviously my ActionScript calls were not very complex in nature).

markerlineAuthor Commented:
Okay, blue-genie:

I found a tut on classes which explains how to create the class and where to locate it relative to your fla file (the class is stored in a .as file which has the same name as the class itself.  I don't understand the part you mentioned which says calling the function from the smallBox by declaring the statement Box(root).changeMyColor();  How does one add a function to an object that is not on the stage in the authoring application but instead created at run-time?  That part is befuddling me.

oooh sorry looks like i was answering someone else's question - too many windows open - you're werent' asking about accessing methods in the document classes.
you aren't even using classes.
i'm sorry - didn't meant to confuse you further.
markerlineAuthor Commented:
Oh, okay.  Thanks for letting me know.  In that case the question remains open and the problem unsolved with accessing functions on Movie Clips spread across several frames.  I will get a null referrence error even though the action script is on frame 1 and spans across the entire movie because in some frames the Movie Clip instances don't exist.  How to isolate that issue and come out on top?

the problem is as i said.
the code gets instantiated in the first frame.
so say you have the following code on frame 1

if (username.text = "Janet") {
//blah blah

but the username textfield is only on the stage at frame 10
it will throw a null reference error.

markerlineAuthor Commented:
Yes blue-genie, I understand that part.  But how can I create a situation where objects appear at different times in the timeline WITHOUT throwing the null reference error.  It seems like this could be done in AS 2.0 but my project requires the use of AS 3.0 since I am importing an InDesign document as an XFL file which uses AS 3.0 .

I have a login at frame 1 and then frame 2 reflects the state of the logged-in individual "You Are Now Logged In, Welcome Janet" where Janet represents the variable name that the person logged in as.  I'm not even getting into password validation yet because that is a bit involved, though ideally I would like Flash to validate the password (not just "if password==logmein" then gotoAndStop(2)" but actual validation against a password that has been registered at the register screen, which in my storyboard is frame 13 or 14).

>>But how can I create a situation where objects appear at different times in the timeline WITHOUT throwing the null reference error.

the correct way.
you dynamically add the item when required using addChild(); //the AS3 standard.

put the item on the stage but hide it.

in your specific instance. in which frame is btnLogIn object?
are all your about/home/nav etc buttons on the stage in the first frame?
can i assume you've imported the required classes but you just haven't shown it in your code snippet.

PS, in future can you paste your code snippet without the line nums so we can select all and paste into IDE For testing.

if you want further assistance can you upload your file and any custom classes you have.
markerlineAuthor Commented:
The FLA file imported the classes automatically at runtime but I did not create any classes as you can see in my code.  I do not have any external AS files.  

Some of my buttons are on the first frame but not all the buttons of the entire InDesign/XFL FLA file.  The btnLogin object should be on that first frame.  I guess I could put items on the stage and hide them, I never really thought about organizing my document in that fashion though.

I have used addChild within this file, but it doesn't always work because if a null reference is found, all the ActionScript gets thrown out the window by the Flash Player and nothing works anymore.  

I can repost the code or attach the CS4 FLA file if you'd like.  EDIT:  No, I can't post the file.  It doesn't allow for the posting of .fla files.  Unfortunately I don't have an FTP site you could download it from either.  What should I do?  Just post the code?  But you won't have the objects that the code represents.
okay. save your file for cS3. then simply add a .txt at the end of the file . ie. filename.fla.txt and upload it.

i don't want to see your code, i want to see your setup. as your setup is your problem, not your code.
markerlineAuthor Commented:
Okay, here's the "txt" file.  Please remember the login information at the top of this post when testing the login process and other processes.

on running your file. these are the errors i get at compile time.

Warning: 5002: The frame scripts of the symbol 'Button' have been ignored because a compiled clip contains a definition for fl.controls.Button. To override the fl.controls.Button definition, place a custom class file within your classpath.

1119: Access of possibly undefined property clickRegister through a reference with static type flash.display:Stage.

the first issue, i don't know what your compile clip is,you never mentioned a problem there so i'll ignore that for now.

the second one you have this code.

your instance is called mcRegister - you're trying to reference it through the class i.e. static which it is not.
you should take that out and put it where the code in on the stage, ie. frame 1 (remove parent reference);

now your runtime null errors.
your problem seems to be the same as teh second problem above.
you need to reference the instance of objects and NOT the class (i.e. the name of the item in the library)

you also need to tidy up your code, there seems to be references of items that arent in use. (or at least i can't find them)
like btnLogOut and btnLogout1

then at about line182 this is throwing the null error

where is logName1?

all the stuff up to that point is fine - just make sure you reference the instance names of objects.
i'd also suggest you clean up the stuff thats not being in use and put the code into the timeline rather than the objects themselves (The listeners) .

from that point on do what i've just been doing and go through each line and see what it is referencing and if that exists.
you're going to have to do further trouble shooting yourself, i have work to do.

go forth and conquer.
markerlineAuthor Commented:

You wrote a lot.  Thanks for all the input.  But this file is a transition file.  The actual file I had problems with before did not have code attached to objects.  The code was all sitting on the timeline.  I tried it on objects based on court_three's input.

The duplicate name references are objects that disappear and then reappear as separate objects elsewhere in the timeline after frame 1.  If you scrolled through the timeline you would see a different page for each stage of browsing through the application.  Those pages are supposed to be accessed by clicking on the buttons that correspond to them (i.e. "gotoAndStop{"register")").

It looks like I'm stuck with the same problem as before and don't really have a solution.  I will continue to get errors as I did before when all of the actions were on frame 1 and spanned across the entire timeline.  :-{  

I don't know where to take it from this point.  But I thank you for your input.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.