错误#1009:无法访问空对象引用的属性或方法。 -as3

时间:2013-11-18 05:57:08

标签: android actionscript-3

我收到了这个错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at TriviaGameDeluxe/saveScore()[TriviaGameDeluxe::frame102:19]

这是我的代码:

//**//
playAgainbutton.addEventListener(MouseEvent.CLICK,saveScore);


function saveScore(event:MouseEvent) { // Save the score


if (savedSN == null) { // Check if a game save is created. If it is not, create one 
trace("New game save created");
savedSN = { // Set the varible 'savedSN'
            name1:"-",
            name2:"-",
            name3:"-",
            name4:"-",

        score1:"-",
        score2:"-",
        score3:"-",
        score4:"-"};
soSavedScNa.data.nameScore = savedSN; // Set the data in the save file to the 
savedSN variable
soSavedScNa.flush(); // Overwrite existing save file

}

    //**// Save the score
        if ((gameScore > savedSN.score1 || savedSN.score1 == "-") && 
gameScore != 0) {
            trace("Score 1");
            savedSN = { // Set the date the savedSN varible will have
                    name1:PName.text,
                    name2:savedSN.name1,
                    name3:savedSN.name2,
                    name4:savedSN.name3,
                score1:gameScore,
                score2:savedSN.score1,
                score3:savedSN.score2,
                score4:savedSN.score3};
            soSavedScNa.data.nameScore = savedSN; // Set the data in 
the save file to the savedSN variable
            soSavedScNa.flush(); // Overwrite existing save file
            playAgainbutton.removeEventListener(MouseEvent.MOUSE_UP, 
saveScore);
            cleanUp();
            gotoAndStop(1); // Go to the start menu
        } else if ((gameScore > savedSN.score2 || savedSN.score2 == "-") && 
gameScore != 0) {
            trace("Score 2");
            savedSN = { // Set the date the savedSN varible will have
                    name1:savedSN.name1,
                    name2:PName.text,
                    name3:savedSN.name2,
                    name4:savedSN.name3,
                score1:savedSN.score1,
                score2:gameScore,
                score3:savedSN.score2,
                score4:savedSN.score3};
            soSavedScNa.data.nameScore = savedSN; // Set the data in 
the save file to the savedSN variable
            soSavedScNa.flush(); // Overwrite existing save file
            playAgainbutton.removeEventListener(MouseEvent.MOUSE_UP, 
saveScore);
            cleanUp();
            gotoAndStop(1);
             // Go to the start menu
        } else if ((gameScore > savedSN.score3 || savedSN.score3 == "-") && 
gameScore != 0) {
            trace("Score 3");
            savedSN = { // Set the date the savedSN varible will have
                    name1:savedSN.name1,
                    name2:savedSN.name2,
                    name3:PName.text,
                    name4:savedSN.name3,
                score1:savedSN.score1,
                score2:savedSN.score2,
                score3:gameScore,
                score4:savedSN.score3};
            soSavedScNa.data.nameScore = savedSN; // Set the data in 
the save file to the savedSN variable
            soSavedScNa.flush(); // Overwrite existing save file
            playAgainbutton.removeEventListener(MouseEvent.MOUSE_UP, 
saveScore);

            cleanUp();
            gotoAndStop(1); // Go to the start menu
        } else if ((gameScore > savedSN.score4 || savedSN.score4 == "-") && 
gameScore != 0) {
            trace("Score 4");
            savedSN = { // Set the date the savedSN varible will have
                    name1:savedSN.name1,
                    name2:savedSN.name2,
                    name3:savedSN.name3,
                    name4:PName.text,
                score1:savedSN.score1,
                score2:savedSN.score2,
                score3:savedSN.score3,
                score4:gameScore};
            soSavedScNa.data.nameScore = savedSN; // Set the data in 
the save file to the savedSN variable
            soSavedScNa.flush(); // Overwrite existing save file
            playAgainbutton.removeEventListener(MouseEvent.MOUSE_UP, 
saveScore);
            cleanUp();
            gotoAndStop(1); // Go to the start menu
        }
    }

我无法找到错误的位置..希望有人可以提供帮助。感谢。

2 个答案:

答案 0 :(得分:0)

在帧102的第19行确实是一个错误。这看起来像是首先引用'soSavedScNa'的行,此时,Flash之前没有听说过该变量。它是一个空对象 - 因此是错误。您需要将其声明为一种变量,并在引用任何实例属性之前对其进行定义。

答案 1 :(得分:0)

您需要实例化对象,找出您未正确声明的对象。

格式正确

obj:object = new object()