Skip to main content
Inspiring
June 23, 2008
Question

Document Class access of button on a specific frame

  • June 23, 2008
  • 9 replies
  • 1484 views
I have a button that exists on a certain frame in my flash movie. I'm getting a null reference error when I try to compile the movie. Here's what the trace window reports:

onConnect1 running! chatService has connected.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MethodInfo-289()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at net.flashmog::FlashMOGService/socketConnectHandler()


The code is attached.
This topic has been closed for replies.

9 replies

kglad
Community Expert
Community Expert
July 1, 2008
run some tests with your sendButton code commented out to check if that's the cause of your null reference error. if the error still occurs comment your removeEventListener() and retest.
sneakyimpAuthor
Inspiring
July 1, 2008
So is that all? My topic is ignored, left to slide into obscurity? A slow death of neglect and never an answer? Have I found a bug in Flash or is there something wrong with my code?
kglad
Community Expert
Community Expert
June 26, 2008
flash is singlethreaded.

are you sure the error never occurs when there's a trace() function?

and how frequently does the error occur when there is no trace() function?
sneakyimpAuthor
Inspiring
June 26, 2008
OK...so I ran it twenty times with the trace(sendButton) function in there

WITH SEND BUTTON TRACE
Total times: 20
With Errors: 12
Without Errors: 8
Error Rate: 60%

I ran it 40 times with no trace function in there
WITH NO TRACE FUNCTION
Total: 40
With Errors: 18
Without Errors: 22
Error Rate: 45%

I'm totally baffled here as to why the behavior would not be consistent. I have attached the code I'm currently running (with the trace function in it) just for reference.
kglad
Community Expert
Community Expert
June 26, 2008
that doesn't make sense unless there's some other timing issue. use trace(sendButton) in place of trace(1) and remove the other trace functions.
sneakyimpAuthor
Inspiring
June 26, 2008
Replacing trace('1') with trace('sendButton') also causes the error to disappear and this is what the trace window says:
onConnect1 running! chatService has connected.
[object Button]

The altered function is attached below. Removing that trace function causes the error to return but not every time. I have noticed that it doesn't consistently appear. We appear to have race conditions of some kind? I don't think I understand how this could happen as Flash is not multithreaded, right?
kglad
Community Expert
Community Expert
June 25, 2008
use some trace() functions to pinpoint the code that's causing that error message.
sneakyimpAuthor
Inspiring
June 25, 2008
I added six trace functions (see attached code) and the error went away. I remove the trace functions and the error comes back. Here's the trace window:

onConnect1 running! chatService has connected.
1
2
6
sneakyimpAuthor
Inspiring
June 24, 2008
Yes that is my document class.

I'm executing stage.invalidate *after* my gotoAnStop statements which seems to work. Why should they be before them?
kglad
Community Expert
Community Expert
June 24, 2008
the invalidate() method needs to execute just prior to the playhead moving.

despite the invalidate() method following your goto method, it does execute before the playhead moves so it will work ok in your situation.
sneakyimpAuthor
Inspiring
June 25, 2008
I tried moving the stage.invalidate calls before the gotoAndStop calls and I get an error. Here's the trace window:

onConnect1 running! chatService has connected.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ChatExample/onRenderChat()


If I leave the stage.invalidate calls *before* the gotoAndStop calls, there are no errors. Perhaps there is something else wrong with my code?
kglad
Community Expert
Community Expert
June 24, 2008
you must use the render event.
sneakyimpAuthor
Inspiring
June 24, 2008
That render event isn't described well anywhere. The adobe documentation barely mentions it and provides little useful info and no meaningful examples here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#event:render

I have the O'Reilly Actionscript 3.0 Cookbook (ISBN-10: 0-596-52695-4) which fails to mention it at all.

I googled around and found something semi-useful by Senocular at kirupa.com:
http://www.kirupa.com/forum/showthread.php?p=1914687

After some experimentation, I have the attached code working without errors but I have no idea if I've called the addEventListener method of the correct object or if I have written some very poor code or what. Any feedback would be greatly appreciated.
kglad
Community Expert
Community Expert
June 24, 2008
you should be in the right scope. (that is your document class, correct?)

just execute stage.invalidate() prior to your gotoAndStop(). (and you can use a trace(submitClick) to confirm your button exists in your onRenderUsername() function.)
sneakyimpAuthor
Inspiring
June 24, 2008
If I'm not mistaken, the playhead should be on that frame because this line is immediately before my attempt to access it:
gotoAndStop('username');

That would take the playhead to the frame with the button (and this is in fact the behavior i see). However, I'm still getting the error.

Am I missing something here?
kglad
Community Expert
Community Expert
June 23, 2008
you can't access it until the playhead enters that frame.