]When is this due to be fixed?I've tried leaving the Chart object alive, instead of nulling out the Chart, but after the first run, it flags nullException 'Oject Reference not set to an Instance .....' on this section, even though debugging shows 96 datapoints exist, and 2 series exist, and Index=0:
Chart1.Series[0].DataPoints[index].YValue = c._OnHoursTotal; // Changing the dataPoint YValue at runtime
Chart1.Series[1].DataPoints[index].YValue = c._OffHoursTotal; // Changing the dataPoint YValue at runtime
public void DailyElecDataSeries()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
Chart1Grid.Children.Clear();
//Chart1 = new Chart();
if (Chart1.Series.Count > 0)
{
Chart1.Series[0].DataPoints.Clear();
if (Chart1.Series.Count>1)Chart1.Series[1].DataPoints.Clear();
Chart1.Series.Clear();
}
Chart1.Width = 400;
Chart1.Height = 200;
Chart1.ScrollingEnabled = false;
Chart1.ShadowEnabled = true;
// Chart1.ColorSet = "VisiOrange";
Chart1.View3D = true;
Chart1.LightingEnabled = true;
Chart1.Background = new SolidColorBrush(Colors.Gray);
Chart1Day = new Title();
Chart1PrevDay = new Title();
Chart1AxisY = new Axis();
//Chart1.Rendered += new EventHandler(Chart1_Rendered);
Chart1.BorderBrush = new SolidColorBrush(Colors.Black);
Chart1.Series.Add(new DataSeries() { RenderAs = RenderAs.StackedColumn });
Chart1.Series.Add(new DataSeries() { RenderAs = RenderAs.StackedColumn });
Chart1.AnimatedUpdate = true;
Chart1.AnimationEnabled = true;
sDate = DateTime.Now.ToShortDateString();
sDate2 = DateTime.Now.AddDays(-1).ToShortDateString();
CO2Multiplier = ObjRefs[CurrentBuilding].ElecCO2;
CarbonMultiplier = ObjRefs[CurrentBuilding].ElecCarbon;
Chart1.Titles.Clear();
Chart1.AxesY.Clear();
//Chart1.AxesX.Clear();
try
{
decimal consumption = 0;
foreach (var c in ElecDayData)
{
decimal dec;
if (Decimal.TryParse(c._day_data.ToString(), out dec))
{
consumption += dec;
}
}
Value1 = Decimal.Round(consumption, 0).ToString() + " kWh";
Value2 = (Decimal.Round(consumption * Convert.ToDecimal(CO2Multiplier), 0).ToString()) + " kg";
//Value3 = (Decimal.Round((Convert.ToInt32((ElecDayData.Compute("sum(day_data)*" + CarbonMultiplier + "", "")))), 2).ToString()) + " kg";
TodayCO2 = Convert.ToDouble(Decimal.Round(consumption * Convert.ToDecimal(CO2Multiplier), 2).ToString());
consumption = 0;
foreach (var c in ElecPrevDayData)
{
decimal dec;
if (Decimal.TryParse(c._day_data.ToString(), out dec))
{
consumption += dec;
}
}
Value3 = Decimal.Round(consumption, 0).ToString() + " kWh";
Value4 = (Decimal.Round(consumption * Convert.ToDecimal(CO2Multiplier), 0).ToString()) + " kg";
//Value6 = (Decimal.Round((Convert.ToInt32((ElecPrevDayData.Compute("sum(day_data)*" + CarbonMultiplier + "", "")))), 2).ToString()) + " kg";
//Chart1AxisY.AxisMaximum = (Math.Max(Convert.ToDecimal(ElecDayData.Max(p => p._day_data.ToString())), Convert.ToDecimal(ElecPrevDayData.Max(p => p._day_data.ToString()) + 1).ToString());
//Also add in the Axis Title at this point,X Axis Title is added in another Method
Chart1AxisY.Title = "kWh";
Chart1.AxesY.Add(Chart1AxisY);
//Chart1.AxesX.Add(Dayaxis);
//Create Chart Titles
Chart1Day.Text = "Electricity Usage Today";
Chart1PrevDay.Text = "Electricity Usage Yesterday";
// Set DataSeries property
Chart1.Series[0].LegendText = "On Hours (kWh)";
Chart1.Series[1].LegendText = "Off Hours (kWh)";
foreach (var c in ElecDayData)
{
dataPoint = new DataPoint();
dataPoint.AxisXLabel = Convert.ToDateTime(c._Date).ToString("HH:mm");
// Add dataPoint to DataPoints collection
Chart1.Series[0].DataPoints.Add(dataPoint);
dataPoint = null;
dataPoint = new DataPoint();
dataPoint.AxisXLabel = Convert.ToDateTime(c._Date).ToString("HH:mm");
// Add dataPoint to DataPoints collection
Chart1.Series[1].DataPoints.Add(dataPoint);
dataPoint = null;
}
if (count == 0)
{
Chart1.Titles.Add(Chart1Day);
int index = 0;
foreach (var c in ElecDayData)
{
// Update YValue property of the DataPoint
Chart1.Series[0].DataPoints[index].YValue = c._OnHoursTotal; // Changing the dataPoint YValue at runtime
Chart1.Series[1].DataPoints[index].YValue = c._OffHoursTotal; // Changing the dataPoint YValue at runtime
index++;
}
}
else
{
Chart1.Titles.Add(Chart1PrevDay);
int index = 0;
foreach (var c in ElecPrevDayData)
{
// Update YValue property of the DataPoint
Chart1.Series[0].DataPoints[index].YValue = c._OnHoursTotal; // Changing the dataPoint YValue at runtime
Chart1.Series[1].DataPoints[index].YValue = c._OffHoursTotal; // Changing the dataPoint YValue at runtime
index++;
}
}
}
catch (Exception ex)
{
string msg = "Daily Electricity Chart Series Rendering Error: ";
msg += ex.InnerException.ToString();
throw new Exception(msg);
}
finally
{
Chart1Grid.Children.Add(Chart1);
//Chart1 = null;
Chart1Day=null;
Chart1PrevDay = null;
Chart1AxisY = null;
}
}
It's better to understand a little, than to misunderstand a lot.