C# ZedGraph对象空引用异常

C# ZedGraph对象空引用异常,c#,C#,我在WinForm的构造函数中使用ZedGraphControl绘制图形时遇到问题,我初始化图形如下: ZedGraphControl Graph = new ZedGraphControl(); Graph.Dock = DockStyle.Fill; GroupBoxGraph.Controls.Add(Graph); GraphPane pane = Graph.GraphPane; /*Initial pane settings*/ pane.XAxis.Type = AxisTy

我在WinForm的构造函数中使用
ZedGraphControl
绘制图形时遇到问题,我初始化图形如下:

ZedGraphControl Graph = new ZedGraphControl(); 
Graph.Dock = DockStyle.Fill;
GroupBoxGraph.Controls.Add(Graph);

GraphPane pane = Graph.GraphPane;

/*Initial pane settings*/
pane.XAxis.Type = AxisType.Date;
pane.XAxis.Scale.Format = "HH:mm:ss";
pane.XAxis.Scale.Min = (XDate)(DateTime.Now);
//Shows 30 seconds interval.
pane.XAxis.Scale.Max = (XDate)(DateTime.Now.AddSeconds(30));
pane.XAxis.Scale.MinorUnit = DateUnit.Second;
pane.XAxis.Scale.MajorUnit = DateUnit.Minute;
pane.XAxis.MajorTic.IsBetweenLabels = true;
pane.XAxis.MinorTic.Size = 5;           

RollingPointPairList list = new RollingPointPairList(1200);
LineItem curve = pane.AddCurve("Hmi Mode", list, Color.Blue, SymbolType.None);

Graph.AxisChange();
tickStart = Environment.TickCount;
为了测试,我想在点击按钮时画一个新的点。所以,点击按钮,我想执行以下代码:

private void button1_Click(object sender, EventArgs e)
    {
        if (Graph != null) {
            // Make sure that the curvelist has at least one curve
            if (Graph.GraphPane.CurveList.Count <= 0)
                return;

            // Get the first CurveItem in the graph
            LineItem curve = Graph.GraphPane.CurveList[0] as LineItem;
            if (curve == null)
                return;

            // Get the PointPairList
            IPointListEdit list = curve.Points as IPointListEdit;
            // If this is null, it means the reference at curve.Points does not
            // support IPointListEdit, so we won't be able to modify it
            if (list == null)
                return;

            // Time is measured in seconds
            double time = (Environment.TickCount - tickStart) / 1000.0;

            // 3 seconds per cycle
            list.Add(time, Math.Sin(2.0 * Math.PI * time / 3.0));

            // Keep the X scale at a rolling 30 second interval, with one
            // major step between the max X value and the end of the axis
            Scale xScale = Graph.GraphPane.XAxis.Scale;
            if (time > xScale.Max - xScale.MajorStep) {
                xScale.Max = time + xScale.MajorStep;
                xScale.Min = xScale.Max - 30.0;
            }

            // Make sure the Y axis is rescaled to accommodate actual data
            Graph.AxisChange();
            // Force a redraw
            Graph.Invalidate();
        }
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
if(图!=null){
//确保曲线列表至少有一条曲线

如果(Graph.GraphPane.CurveList.Count您的第一个代码块看起来像是在创建名为
Graph
的局部变量,但是您的第二个代码块
按钮1\u单击
看起来像是在尝试使用类字段(或属性)调用
Graph
。这是两个不同的实体。因此,初始化代码可能从未将
ZedGraphControl
的实例分配给
Graph
字段,因此它总是
null

尝试更改第一个代码以删除局部变量声明,并改为引用该字段。也就是说,将第一行更改为:

Graph = new ZedGraphControl();

您的第一个代码块看起来像是在创建一个名为
Graph
的局部变量,但您的第二个代码块
按钮1\u单击
看起来像是在尝试使用一个类字段(或属性)调用
Graph
。这是两个不同的实体。请尝试更改第一个代码以删除局部变量声明,并改为引用该字段(即,将第一行更改为:
Graph=new ZedGraphControl();
),如果您确定情况并非如此,请发布您正在使用的实际代码。