ActionScript error: TypeError: Error #1009: Cannot access a property ...

-- "2 buttons.txt"  inside the attached zip is actually "2 buttons.fla", please extract and rename.

In the attached fla file, click on "bt1' takes the timeline to frame 2. click on "bt2" returns to frame 1
and thrown the error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at 2buttons_fla::MainTimeline/frame1()
      at flash.display::MovieClip/gotoAndStop()
      at 2buttons_fla::MainTimeline/gotoFrame1()

from this point, bt1 is not functioning anymore.
Now I've found that if bt1 does not contain any other symbol inside it (currently it contains a movie clip), the problem does not appear.
import flash.events.MouseEvent;
stop();
 
this.bt1_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
this.bt2_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame1);
this.bt3_instance.bt3_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
 
function gotoFrame1(event:MouseEvent):void{
gotoAndStop(1);
}
 
function gotoFrame2(event:MouseEvent):void{
gotoAndStop(2);
}

Open in new window

tzsysadminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rascalpantsCommented:
this probably will not help the issue, but I would use CLICK instead of MOUSE_DOWN...

next, I would do a ctrl + shift + enter and look what line number the error happens on in the debug mode.

this will give you a better idea about where to start looking...


rp / ZA
0
tzsysadminAuthor Commented:
Thanks.
using Click does make any difference.
The error occures when reaching line 4:
this.bt1_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
0
rascalpantsCommented:

"using Click does make any difference."

im confused... it does or doesn't make any difference?
 
using CLICK opens up your device posibilities to more than just computers with a mouse...  it is just better practice to be more general.


and if that line is where the error is occuring, then the issue is either your callback function is not named correctly, or your instance name is named incorrectly, or you have not properly imported the Event classes...

try using the wild card import flash.display.*;


rp / ZA

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

tzsysadminAuthor Commented:
sorry for the typo i meant "using click does not make any difference".
I accept the facy that Click is better any how.
importing flash.display.* still does not help.

I don't think there's a problem with my code / callback function.
As you can see in my question, this is a strange situation that only occurs if the target button (bt1) has a movie clip inside it. It happens on line 4 because this line refers to bt1, but any other piece of code that would do that will cause the error.
0
rascalpantsCommented:
just to make sure of one thing...

is the instance name of the button "bt1_instance" or "bt1" in the properties panel?

if it is bt1, then you need to get rid of the _instance part...  I ask this, because in your last comment you called the button bt1, not bt1_instance.


rp / ZA
 
0
tzsysadminAuthor Commented:
The instance name is bt1_instance as written in the code.
Note that the code works fine when the movie starts - there isn't any problem accessing the button or setting the event listener. The error only occurs when jumping to frame 2 and returning to frame 1.

thanks
0
rascalpantsCommented:
what code is on frame 1 and frame 2?

I have a feeling that you are instantiating the code again if you go back to frame 1, that has all of this code on it.  

I would probably put the visual assets from both frames and move them over one.  then make sure your actions layer extends over all three frames...


rp / ZA
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blue-genieCommented:
hey guys.
i've duplicated your code and I don't get any errors.
where is this zip file you mentioned - i think like rp says you've got your setup a bit funny.
0
rascalpantsCommented:

yeah, the code would work fine if no code was on Frame 1, but as I said, I can imagine that instantiating this code multiple times will cause an error or the application to act funny


rp / ZA
0
tzsysadminAuthor Commented:
I've tried to attach a file to the question but it does not appear.
Anyway, the file I'm talking about can be downloaded from:

http://www.box.net/shared/77a4hvcn7s

It's a simple 2 frames, 2 button fla with the code in question.
I'm not sure I understand the "setup" issue, so if you could take a look at the file, that would be great.
Thanks
0
rascalpantsCommented:

okay... I am not sure how to better explain this, but you cannot put code on frame 1 and then have the button send the playhead back to frame one again.  this seems to be your problem.

you need to move your assets over one frame each.


rp / ZA


0
tzsysadminAuthor Commented:
So where should I put the code?

I've tried putting the code of
------
function gotoFrame1(event:MouseEvent):void{
gotoAndStop(1); }
------
in frame 2, the result was just the same.

The scenrio is simple:
frame 1 has a button to jump to frame 2 and frame 2 has a button to jump to frame 1. How should I implement this?

thanks
0
rascalpantsCommented:
what I am saying is that you should put the code on frame one, but you should never have your application go to frame one ever again, by using the gotoAndStop(1) code.

if the playhead hits that frame 1 again, then it restarts everything, which is probablby your issue...

rp / ZA
0
rascalpantsCommented:
a solution to the problem was given, but the Author just could not figure out how to implement.

rp
0
rascalpantsCommented:
I think either 3 or 4 should be the result of this question, and mainly because having this PAQed would give other members of the community insight into how not to structure an application.  

ID: 24494840

that is the first comment of a few that tried to explain that a possible problem lies with having your code get re-run each time you go back to frame one.

this application should instead all be on frame one, and the buttons should control inner movieclips.


rp
0
tzsysadminAuthor Commented:
I do not except the solution of having the code in one frame and the componenet on other frames.
The reason is simple: The code should access components (buttons) which does not exist in all of the frames. I have two ket frames, each with different buttons and different code. If I put the code in a third frame (say frame 1 has the code, and frames 2 & 3 contain the buttons), frame 1 does not contain the buttons in frames 2 & 3 and I simply cannot refer to the button instances. The alternative of have those instances in frame 1 and moving them around (say out of stage) is poor.
0
blue-genieCommented:
my 2cs worth.
what rp said is correct. You're getting the error because when you return to frame 1 from frame 2, the code is kicking in before the sprite is reinstantiated, so it doesn't recognize it and throws an error when you try to reference it with the addEventListener.

Here's a work around if you don't want to change your frame setup.



 
import flash.events.MouseEvent;
import flash.display.*;
 
if (bt1_instance!=null) {
	bt1_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
}
bt2_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame1);
bt3_instance.bt3_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
 
 
function gotoFrame1(event:MouseEvent):void {
 
	gotoAndStop(1);
	bt1_instance.addEventListener(MouseEvent.MOUSE_DOWN,gotoFrame2);
}
 
function gotoFrame2(event:MouseEvent):void {
 
	gotoAndStop(2);
 
}
stop();

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.