收到错误空引用异常已处理

发布于 2024-11-25 16:30:49 字数 2113 浏览 1 评论 0原文

我尝试使用以下代码单击一个图表来显示报告图表,

但它显示错误

错误:已处理空引用异常 你调用的对象是空的。在这一行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-12-02 16:30:49

您设置了一个变量

Chart targetcontrol = null;

,然后尝试使用

targetcontrol.ChartAreas.Clear();

targetcontrol 为 null 并且没有 ChartAreas 属性。尝试将第三行更改

Chart targetcontrol = null;

Chart targetcontrol = new Chart();

You set a variable

Chart targetcontrol = null;

then trying to use

targetcontrol.ChartAreas.Clear();

targetcontrol is null and don't have ChartAreas property. Try change the third line

Chart targetcontrol = null;

to

Chart targetcontrol = new Chart();
水波映月 2024-12-02 16:30:49

当然你会得到这个错误,你声明

Chart targetcontrol = null; 

然后你试图使用它而不实际将它实例化为任何东西。

Of course you get that error, you're declaring

Chart targetcontrol = null; 

Then you're trying to use it without actually instantiating it to anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文