C# 向图表添加点时出现Nullreferenceexception

C# 向图表添加点时出现Nullreferenceexception,c#,winforms,graph,nullreferenceexception,C#,Winforms,Graph,Nullreferenceexception,创建图表: private Chart mainChart; private void createChart() { mainChart = new Chart { Dock = DockStyle.Fill, Name = "chart1", }; mainChart.ChartAreas.Add(new ChartArea()); main

创建图表:

    private Chart mainChart;
    private void createChart()
    {
        mainChart = new Chart
        {
            Dock = DockStyle.Fill,
            Name = "chart1",
        };
        mainChart.ChartAreas.Add(new ChartArea());
        mainChart.Legends.Add(new Legend());

        mainChart.ChartAreas[0].AxisX.Minimum = 1;
        for (int i = 0; i < 32; i++)
        {
            mainChart.Series.Add(new Series
            {
                Name = "test" + i,
                LegendText = "Test " + (i + 1).ToString().PadLeft(2, '0'),
                ChartType = SeriesChartType.FastLine,
            });
        }
    }
形式预装法:

private Form FormAlreadyLoaded(string formName)
{
    foreach (Form frm in this.MdiChildren)
        if (frm.Name.Equals(formName))
            return frm;
    return null;
}
Child_图表是一种简单的形式,在“加载”方法中使用:

添加到图表(错误发生的位置)

例外文本:

    System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataVisualization.Charting.Chart.get_Series()
   at Oasis.MainForm.<>c__DisplayClass13.<ProcessSerial>b__11() in MainForm.cs:line 355
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
在serialPort的DataReceived事件中

出现错误后“mainChart”的本地值i.imgur.com/LZSY3Iw.png


所有这些代码都可以正常工作,直到我关闭“Child_Chart”。

因为系列[I]的Points属性可能是非原始类型,您需要使用其构造函数使用“new”关键字初始化它。

您确定
Chart
不是空值吗?我看不到调用
CreateChart()
的地方,也看不到私有字段
mainChart
被实例化的任何地方


除此之外,您需要打断错误并查看哪个是空引用。整个语句可以是
chart
chart.Series
,或
chart.Series[i]
,等等。

我在主帖子中添加了更多信息。我在主帖子中添加了更多信息。
MainForm parent = this.MdiParent as MainForm;
this.Controls.Add(parent.chart);
chart.Series[i].Points.AddXY(value1,value2);
    System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataVisualization.Charting.Chart.get_Series()
   at Oasis.MainForm.<>c__DisplayClass13.<ProcessSerial>b__11() in MainForm.cs:line 355
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
this.BeginInvoke(new MethodInvoker(delegate()