2

在调试代码时,出现错误#NULL is not a valid value for Int32

private void satelliteComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    Helper.SetWaitCursor();
    if (satelliteComboBox.SelectedValue != null)
    {
        var satelliteId = Convert.ToInt32(satelliteComboBox.SelectedValue);
        satelliteStatusUserControl.DataSource = 
            _satelliteStatusBusinessService.GetSingleSatellite(
                new Dictionary<string, object> { { "SatelliteID", satelliteId } }, true);
        //2012.07.07 get colors for all machine status
        satelliteStatusUserControl.DataSource.DefectColors = 
                  _satelliteStatusBusinessService.GetDefectColors().ToList();

        foreach (var defectColor in 
                      satelliteStatusUserControl.DataSource.DefectColors)
        {
            MachineStatusCtrl.AddMachineStatusColors(
                         defectColor.DefectTypeID, 
                         defectColor.DefectType, 
                         defectColor.OEEColor);
        }
        //2012.07.07
        satelliteStatusUserControl.DataBind();
    }
    Helper.SetDefaultCursor();
}

我在foreach循环中收到此错误

更新:这是实现AddMachineStatusColors

public static void AddMachineStatusColors(int statusColorId, string StatusName, string oeeColor)
{         
    MacStatusColors macStatusColor;

    //add dummy colors with unknow till the next defectId so that it will be easy to get color later while painting.            
    for(int Index = StatusColors.Count; Index < statusColorId ; Index++)
    {
        macStatusColor = new MacStatusColors();
        StatusColors.Add(macStatusColor);
    }

    macStatusColor = new MacStatusColors();
    macStatusColor.DefectTypeID = statusColorId;
    macStatusColor.DefectType = StatusName;
    macStatusColor.OEEColor1 = ControlPaint.Dark(getColorFromString(oeeColor));
    macStatusColor.OEEColor2 = ControlPaint.Light(getColorFromString(oeeColor));
    StatusColors.Add(macStatusColor);
}



    public static Color getColorFromString(string oeeColor)
    {            
        if (oeeColor[0] != '#') { oeeColor = '#' + oeeColor; }
        return System.Drawing.ColorTranslator.FromHtml(oeeColor);
    }
4

3 回答 3

2

好吧,defectColor.OEEColor似乎是null,并且您的数据模型不允许该值是null. 三种可能的解决方案:

  1. 更改数据模型,以便字段可以null
  2. 确保defectColor.OEEColor不是null
  3. 将行更改为:

    MachineStatusCtrl.AddMachineStatusColors(..., ..., defectColor.OEEColor ?? <default value>);

为 0 或您想用来表示“颜色为”的<default value>任何其他值。intnull


您添加了该AddMachineStatusColors方法的代码。谢谢,但我仍然说您的代码假定defectColor.OEEColor不是null. 您的代码中至少有两行我认为defectColor.OEEColor可能不是null

macStatusColor.OEEColor1 = ControlPaint.Dark(getColorFromString(oeeColor));
macStatusColor.OEEColor2 = ControlPaint.Light(getColorFromString(oeeColor));

请显示 的代码,或准确告诉我们错误发生getColorFromString在您方法的哪一行!AddMachineStatusColors


好的,现在我们到了某个地方。defectColor.OEEColor包含字符串值“null”!!这意味着,defectColor.OEEColor它本身不是null,但它包含单词 "null"

因此,以下行将单词“null”转换为“#null”(因此您关于“#null is not a valid value...”的奇怪问题标题):

if (oeeColor[0] != '#') { oeeColor = '#' + oeeColor; }

之后oeeColor具有值“#null”,然后以下行(我想,因为您没有向我们提供有关异常真正发生的行的任何详细信息......)引发错误:

return System.Drawing.ColorTranslator.FromHtml(oeeColor);

您必须确保defectColor.OEEColor两者都不是null(即“没有值”)并且它的值是有效的 HTML 颜色字符串!

于 2013-05-02T07:21:15.237 回答
0

如果您收到的整数可能为空,则可能是 aNullable<Integer>这意味着它将具有一个.HasValue属性...

foreach (var defectColor in satelliteStatusUserControl.DataSource.DefectColors)
{
    if(defectColor.OEEColor.HasValue) {
        MachineStatusCtrl.AddMachineStatusColors(defectColor.DefectTypeID, defectColor.DefectType, defectColor.OEEColor);
    } else {
        //Use a default
        MachineStatusCtrl.AddMachineStatusColors(defectColor.DefectTypeID, defectColor.DefectType, 0);
    }
}
于 2013-05-02T07:23:23.160 回答
-1
int? satelliteId;
int i;
if (satelliteComboBox.SelectedValue != null && int.tryParse(satelliteComboBox.SelectedValue, out i))
{
    satellideId = i;
}
于 2013-05-02T07:25:03.053 回答