Reputation: 71
While I am debugging the code, I am getting the error #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();
}
I am getting this error in my foreach
loop
UPDATE: Here is the implementation for 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);
}
Upvotes: 2
Views: 8444
Reputation: 56727
Well, defectColor.OEEColor
seems to be null
and your data model does not permit the value to be null
. Three possible solutions:
null
defectColor.OEEColor
is not null
Change the line to:
MachineStatusCtrl.AddMachineStatusColors(..., ..., defectColor.OEEColor ?? <default value>);
With <default value>
being 0 or any other int
value that you want to use to indicate "color is null
".
You added the code for the AddMachineStatusColors
method. Thanks, but still I say that your code assumes that defectColor.OEEColor
is not null
. There are at least two lines in your code where I assume that defectColor.OEEColor
may not be null
:
macStatusColor.OEEColor1 = ControlPaint.Dark(getColorFromString(oeeColor));
macStatusColor.OEEColor2 = ControlPaint.Light(getColorFromString(oeeColor));
Show the code for getColorFromString
, please, or tell us exactly on which line in your AddMachineStatusColors
method the error occurs!
OK, now we're getting somewhere. defectColor.OEEColor
contains the string value "null"!! That means, defectColor.OEEColor
itself is not null
, but it contains the word "null".
Because of this, the following line turns the word "null" into "#null" (thus your strange question headline about "#null is not a valid value..."):
if (oeeColor[0] != '#') { oeeColor = '#' + oeeColor; }
After that oeeColor
has the value "#null" and then the following line (I suppose, as you've not given us any details on which line the exception really occurs...) throws an error:
return System.Drawing.ColorTranslator.FromHtml(oeeColor);
You must make sure that defectColor.OEEColor
is neither null
(i.e. "doesn't have a value") and that the value it has is a valid HTML color string!
Upvotes: 2
Reputation: 4914
int? satelliteId;
int i;
if (satelliteComboBox.SelectedValue != null && int.tryParse(satelliteComboBox.SelectedValue, out i))
{
satellideId = i;
}
Upvotes: -1
Reputation: 26766
If the integer you're receiving might be null, it's probably a Nullable<Integer>
which means it will have a .HasValue
property...
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);
}
}
Upvotes: 0