Search code examples
flashactionscript-3scrollpaneflash-v3-components

null object reference in ScrollPane/endDrag() when scrollDrag=true


In my flash application, I've got multiple windows which use Scrollpanes. The scrollDrag property is set to true on these because I want that functionality. If I close (within my application) one of these 'windows' and open another, I seem to get a whole lot of this error showing up in my logs:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.containers::ScrollPane/endDrag()

Sometimes I get thousands of these, which I'm guessing is probably slowing my app down a bit, but otherwise is not causing a problem. Looking through the adobe code for scrollpane, endDrag is really simple:

protected function endDrag(event:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
    }

The stage var is the only thing that could be null here.

The only thing I can think to do is set scrollDrag=false before the window in my application closes so that nothing is listening for the event. Any other suggestions?


Solution

  • I've tried to recreate your scenario, so I had a dummy clip to be loaded by the ScrollPane, and the ScrollPane contained withing a MovieClip with linkage(Export for Actionscript) so I can create several instances. Also in that clip, a layer above the ScrollPane component I've placed a close button.

    My first attept was to debug the fla and see where exactly it fails first. I didn't mange to find out anything this was as I kept getting this:

    Cannot display source code at this location.
    

    Then I followed your instructions, and found the endDrag() function. I changed it to this:

    protected function endDrag(event:MouseEvent):void {
                if(stage) stage.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
            }
    

    And tried it. It didn't work the first time, as if it was not compiled. I tried to edit the class within the Flash IDE and I saw what this little caveat was all about. Here's what I mean:

    ScrollPane edit

    So I copied ScrollPane.as from the Flash CS4 folder into ./fl/containers/ScrollPane (basically relative to the .fla). This .as file got compiled, and the error was gone.

    Short version is: Yup! you found the problem spot :) Add an if to check for null object as a quickfix and don't forget to save ScrollPane.as relative to the .fla file or in your classpath before compiling again.

    HTH, George