Skip to main content
Known Participant
July 11, 2013
Answered

Null Object Reference Error Problem

  • July 11, 2013
  • 1 reply
  • 1133 views

Hello, I'm having a problem with a class(_gameModel), being instantiated in another class(_gameController), and when I try to reference the _gameModel class in the _gameController class I get a null Object reference error even though I know that the class is instantiated at the top of the _gameController class page.  I thought that if it was instantiated in the class then I wouldn't have this problem but I am. Anyone know what the problem is? thanks

This topic has been closed for replies.
Correct answer Ned Murphy

I don't see anything being instantiated relative to anything you've indicated that is, so I can't say if that is an issue or not. 

Did you show only a portion of the error message or is there more information in it?  Show the complete error message if that is not all of it.

Based on that function involving "EnterFrame" in the name, my suspiscions fall to wondering if you are catching that error after the object has been removed on a previous pass thru the function.  How do the traces look from that first line in the function?  Does is come up as null when the error occurs but looks okay before that?

1 reply

Ned Murphy
Legend
July 11, 2013

Show the code related to this issue as well as the complete error message.  BEfore showing the message, go into your Flash Publish Settings and select the option to Permit Debugging so that the error message might be more complete with the info it offers.

Known Participant
July 11, 2013

Here's the game loop function where I am having the problem and below that is the function where added the _pauseSound to the _gameModel.screen Sprite. I first tried to remove _pauseSound with this:

_gameModel.screen.removeChild(_pauseSound); but I get a #2025 Error: supplied DisplayObject must be a child of the caller, so I changed it to _pauseSound.parent.removeChild(_pauseSound); and I get the null object error. I have _pauseSound instantiated at the top of the page so I don't know what the problem is.

public function runGameEnterFrame(e:Event):void {

                              trace("_pauseSound = "+_pauseSound);

                                        screenText.update();

                                        timeDifference = getTimer() - lastTime;

                                        lastTime = getTimer();

                                        checkSystemState();

                                        systemFunction();

                                        frameCounter.countFrames();

                                        if(_gameModel.updateMusVol){

                                                  soundController.updateSoundVolume(true);

                                        }

                                        if(_gameModel.updateSoundVol){

                                                  soundController.updateSoundVolume(false);

                                        }

                                        if(_gameModel.gotoIntro){

                                                  _docClass.gotoAndStop(1);

                                                  trace("_pauseSound2 = "+_pauseSound);

                                                  trace("_pauseSound.parent = "+_pauseSound.parent);-------This is 'null'

                                                  trace("_gameModel.screen = "+_gameModel.screen );

                                                  _pauseSound.parent.removeChild(_pauseSound);-----error

                                                  _gameModel.pauseAndMuteAdded = false;

                                                  _levelTwo.dispose();

                                                  _gameModel.gotoIntro = false;

                                                  }

                    }

public function addPauseEtc():void{

                              if(_gameModel.pauseAndMuteAdded == false){

 

                                                  _pauseSound.addPauseSoundButtons(470,20,500,20,540,20,580,20);

                                                            _gameModel.screen.addChild(_pauseSound);

                                                            _gameModel.pauseAndMuteAdded = true;

                                                  }

                    }

Ned Murphy
Ned MurphyCorrect answer
Legend
July 12, 2013

I don't see anything being instantiated relative to anything you've indicated that is, so I can't say if that is an issue or not. 

Did you show only a portion of the error message or is there more information in it?  Show the complete error message if that is not all of it.

Based on that function involving "EnterFrame" in the name, my suspiscions fall to wondering if you are catching that error after the object has been removed on a previous pass thru the function.  How do the traces look from that first line in the function?  Does is come up as null when the error occurs but looks okay before that?