简体   繁体   English

ZedGraph对象nullreference异常

[英]ZedGraph object nullreference exception

I have a problem plotting a graph using the ZedGraphControl in the constructor of my WinForm I initialize the graph like this: 我在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;

And just for testing I want to plot a new point when i click a button. 仅出于测试目的,我想在单击按钮时绘制一个新点。 So on button click I want to execute this code: 所以在按钮上单击我要执行以下代码:

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();
        }
    }

But my Graph Object is always null! 但是我的图对象总是空的! I even created a property and put a breakpoint in the setter. 我什至创建了一个属性,并在设置器中放置了一个断点。 The setter is never called but the object is still null. 该设置器从不调用,但该对象仍为null。 (After the first line of code i posted the Graph object is not null. (在我发布的代码的第一行之后, Graph对象不为null。

Any idea how this can happen? 知道如何发生吗? Thank you 谢谢

Your first code block looks like it's creating a local variable called Graph , but your second code block button1_Click looks like it's trying to use a class field (or property ) called Graph . 您的第一个代码块似乎正在创建一个名为Graph局部变量 ,但是您的第二个代码块button1_Click似乎正在尝试使用一个名为Graph类字段 (或属性 )。 These are two different entities. 这是两个不同的实体。 So likely your initializing code is never assigning an instance of ZedGraphControl to the Graph field , thus it's always null . 因此,您的初始化代码很可能从未将ZedGraphControl的实例分配给Graph 字段 ,因此它始终为null

Try changing your first code to remove the local variable declaration and reference the field instead. 尝试更改您的第一个代码以删除局部变量声明,然后引用该字段。 That is, change the first line to: 也就是说,将第一行更改为:

Graph = new ZedGraphControl();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM