收到错误空引用异常已处理
我尝试使用以下代码单击一个图表来显示报告图表,
但它显示错误
错误:已处理空引用异常 你调用的对象是空的。在这一行 targetcontrol.ChartAreas.Clear();
这是图表控件的单击事件
using System.Windows.Forms.DataVisualization.Charting;
private void kpiChartControl_Click(object sender, EventArgs e)
{
Chart targetcontrol = null;
Series series = null;
Title title;
string are;
targetcontrol.ChartAreas.Clear();
targetcontrol.Series.Clear();
targetcontrol.Titles.Clear();
DataTable accepts = null;
accepts = KPIData.AcceptedvisitsByMembership(mf ,"accepted");
are = " acceptedvisitsmshiptypes";
targetcontrol.ChartAreas.Add(are);
series = targetcontrol.Series.Add(are);
series.ChartArea = are;
title = targetcontrol.Titles.Add("Accepted Visits By MemberShip Type");
title.DockedToChartArea = are;
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
foreach (Title titles in targetcontrol.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (Series serie in targetcontrol.Series)
{
serie.ChartType = SeriesChartType.Pie;
serie["PieLabelStyle"] = "Outside";
serie["DoughnutRadius"] = "30";
serie["PieDrawingStyle"] = "SoftEdge";
serie.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Legend legend in targetcontrol.Legends)
{
legend.Enabled = false;
}
foreach (ChartArea chartArea in targetcontrol.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.Inclination = 45;
//chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
}
targetcontrol.Series[0].Points.DataBindXY(accepts.Rows, "mshiptypenumbers", accepts.Rows, "mshipType_Name");
foreach (Series chartSeries in targetcontrol.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Silver membership": point.Color = Color.Green; break;
//case "Refused": point.Color = Color.Red; break;
}
point.Label = string.Format("{0:0}", point.YValues[0]);
}
}
}
I am trying to show the reporting chart by clicking on the one chart by using following
code but it was showing an error
ERROR : null reference exception was Handled
Object reference not set to an instance of an object. at this line targetcontrol.ChartAreas.Clear();
and this is click event for chart control
using System.Windows.Forms.DataVisualization.Charting;
private void kpiChartControl_Click(object sender, EventArgs e)
{
Chart targetcontrol = null;
Series series = null;
Title title;
string are;
targetcontrol.ChartAreas.Clear();
targetcontrol.Series.Clear();
targetcontrol.Titles.Clear();
DataTable accepts = null;
accepts = KPIData.AcceptedvisitsByMembership(mf ,"accepted");
are = " acceptedvisitsmshiptypes";
targetcontrol.ChartAreas.Add(are);
series = targetcontrol.Series.Add(are);
series.ChartArea = are;
title = targetcontrol.Titles.Add("Accepted Visits By MemberShip Type");
title.DockedToChartArea = are;
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
foreach (Title titles in targetcontrol.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (Series serie in targetcontrol.Series)
{
serie.ChartType = SeriesChartType.Pie;
serie["PieLabelStyle"] = "Outside";
serie["DoughnutRadius"] = "30";
serie["PieDrawingStyle"] = "SoftEdge";
serie.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Legend legend in targetcontrol.Legends)
{
legend.Enabled = false;
}
foreach (ChartArea chartArea in targetcontrol.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.Inclination = 45;
//chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
}
targetcontrol.Series[0].Points.DataBindXY(accepts.Rows, "mshiptypenumbers", accepts.Rows, "mshipType_Name");
foreach (Series chartSeries in targetcontrol.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Silver membership": point.Color = Color.Green; break;
//case "Refused": point.Color = Color.Red; break;
}
point.Label = string.Format("{0:0}", point.YValues[0]);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您设置了一个变量
,然后尝试使用
targetcontrol 为 null 并且没有 ChartAreas 属性。尝试将第三行更改
为
You set a variable
then trying to use
targetcontrol is null and don't have ChartAreas property. Try change the third line
to
当然你会得到这个错误,你声明
然后你试图使用它而不实际将它实例化为任何东西。
Of course you get that error, you're declaring
Then you're trying to use it without actually instantiating it to anything.