运行我的代码时引发TypeError

时间:2012-10-02 13:31:56

标签: actionscript-3

我尝试运行代码时出现以下错误。

  

TypeError:错误#1010:术语未定义且没有属性。

    private function calculateFIX001Value(custfields:Array):Number
    {
        var result:Number = new Number;
        var valueObj:Object;

        try{
                var markUp:Number = 0.01;
                var projectValue:String = "";
                var marketCategory:String = "";

                for each (var custField:Object in custfields)
                {
                    if (custField.fieldLabel == 'Segment Category')
                        marketCategory = custField.selectedValue[0];

                    if (custField.fieldLabel == 'Project Value (R)')
                        projectValue = custField.selectedValue[0].toString().replace("R","");
                        projectValue = projectValue.replace(",","");
                        projectValue = projectValue.replace(",","");
                        projectValue = projectValue.replace(",","");
                }

                switch (marketCategory)
                {
                    case "23": //5% Markup for Building
                        markUp = new Number(5 / 100);
                        break;
                    case "24": //11% Markup for Civils
                        markUp = new Number(11 / 100);
                        break;
                    case "25": //2% Markup for Scaffolding
                        markUp = new Number(2 / 100);
                        break;
                    default:
                        break;
                }

                if ((projectValue != "" && projectValue != '0') && marketCategory != "")
                {
                    var revenue:Number = new Number;
                    //if (!_model.myProfile.switchOpportunityInMillions)
                    //  revenue = parseFloat(projectValue) * 1000000 * markUp;
                    //else
                        revenue = parseFloat(projectValue) * markUp;
                    valueObj = new Object;
                    valueObj.label = "Expected Revenue";
                    valueObj.value = revenue;
                    //result.push(valueObj);
                    result = revenue;
                    //rm.saveOpportunityValue(_currentOpportunity.id, revenue);
                }
                else
                {
                    var errorObj:Object = new Object;
                    var errMsg:String = "";

                    if (projectValue == '' || projectValue == '0')
                        errMsg = "Project value not specified";

                    if (marketCategory == "")
                    {
                        if (errMsg != "")
                            errMsg += " or no Segment Category has been selected";
                        else
                            errMsg = "No Segment Category has been selected";
                    }
                    errorObj.label = 'Cannot calculate value.';
                    errorObj.value = errMsg + '.';
                    //result.push(errorObj);

                    result = 0;
                }
        } catch(e:Error) {
            Alert.show(e.toString());
        }

        return result;
    }

2 个答案:

答案 0 :(得分:2)

TypeError:错误#1010也称为“空指针异常”,这意味着您尝试访问空对象属性。

如果你看一下堆栈跟踪告诉你的那一行,你可能会找到没有太大问题的原因。

答案 1 :(得分:0)

这是一个帮助您解决问题的间接答案:(因为代码中有很多东西可能是null或未定义并导致此错误)

你几乎所有的代码都在try/catch中,所以你的错误实际上并没有被抛出,你只是在追踪被捕获的错误。

抛出错误(通过删除try / catch或在catch块中手动抛出它):

catch(e:Error) {
    Alert.show(e.toString()); //instead of this, for debugging, do the line below:
    Throw e; //this will throw the error, so you will get your stack trace and the line number of error
}

要获取错误的行号并查看堆栈跟踪,您需要在调试播放器或IDE(例如Flash或FlashBuilder)中运行。