if (this.chart1.Series.IndexOf(strSerieName) < 0)
                {
                    this.chart1.Series.Add(strSerieName);
                    this.chart1.Series[strSerieName].ChartType = sctType;
                    this.chart1.Series[strSerieName].Color = color;
                    this.chart1.Series[strSerieName].BorderWidth = iWidth;
                    return true;
                }程序执行上面代码的时候抛出了如下异常:
'this.chart1.Series' threw an exception of type 'System.NullReferenceException'请问各位,该怎么解决呢。

解决方案 »

  1.   

    chart1对象没有实例化呢,是个空对象
      

  2.   

    chart1实例化了,是Series抛出的异常哦
      

  3.   

    应该this.chart1.Series没有实例化,这样写吧:
    Excel.Series oSeries;
    不对,我估计你这里应该有个EXCEL操作类吧,你引用了这个EXCEL操作类,因为EXCEL类库里chart是没有.Series方法的,你最好把你的那个EXCEL操作类贴下。要不可不好给你解决。
    你要不看下我自己的一段代码,直接用EXCEL类库的:
    //以下是定义数据系列和数据标签格式的,系列边框,标签字体大小等
                workbook.ActiveChart.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowValue, false, false, false,true, true, true, false, false, false);//这里workbook已经是实例了
                Excel.Series oSeries;
                Excel.DataLabel oDLabel;
                for (int i = 0; i < ZoneListBox.Items.Count; i++)
                {
                    oSeries = (Excel.Series)workbook.ActiveChart.SeriesCollection(i + 1);
                    oSeries.HasDataLabels = true;
                    oSeries.Border.ColorIndex = 45;
                    oSeries.Name="xxxxxx";//xxxxxx就是想写的数据系列名称                oDLabel = (Excel.DataLabel)(((Excel.Series)workbook.ActiveChart.SeriesCollection(i + 1)).DataLabels(1));
                    //oDLabel.AutoScaleFont = false;
                    oDLabel.Font.Name = "Times New Roman";
                    //oDLabel.Font.Size = 12;
                }
      

  4.   

    没可能的,用法确实是这么用。你这样试下看看就知道什么地方出问题了
    if (this.chart1.Series.IndexOf(strSerieName) < 0)
      {
      this.chart1.Series.Add(strSerieName);
      this.chart1.Series[strSerieName].ChartType = sctType;
      this.chart1.Series[strSerieName].Color = color;
      this.chart1.Series[strSerieName].BorderWidth = iWidth;
      return true;
      }改为if (this.chart1.Series.FindByName(strSerieName) != null) //??是否需要呢??
      {
      this.chart1.Series.Add(strSerieName);
      this.chart1.Series[this.chart1.Series - 1].ChartType = sctType;//此处此时应该不再报异常了吧
      this.chart1.Series[strSerieName].Color = color;
      this.chart1.Series[strSerieName].BorderWidth = iWidth;
      return true;
      }
      

  5.   

    我不是说你用法问题,而是Microsoft.Office.Interop.Excel,我查找了似乎真没有那个方法啊,你能这么用应该是用了别人在Microsoft.Office.Interop.Excel基础上写的excel操作类吧,又或者是第三方控件啊。你最好看下,然后把那个贴下,要不不好帮你看啊。
      

  6.   

    这个应该是个MsChart控件来的吧?微软后来出来的一个控件,需要额外安装的。LZ我给你看下我些的一个函数,这个函数用了很长一段时间了都没有问题,你看下
            private Series AddChartSeries(string staid, string sDate, string dataType, string unit, Chart thisChart)
            {
                StringBuilder title = new StringBuilder(100);
                title.Append(DateTime.Parse(sDate).AddDays(-1).ToString("yyyy-MM-dd"));
                title.Append(" 至 ");
                title.Append(sDate);
                title.Append(" 水文站");
                title.Append(dataType);
                title.Append("图(单位:");
                title.Append(unit);
                title.Append(")");
                if (thisChart.Titles.Count == 0)
                {
                    //如果第一次读取则设置标题
                    thisChart.Titles.Add(title.ToString());
                }
                else
                {
                    thisChart.Titles[0].Name = title.ToString();
                }
                thisChart.Series.Add(staid);
                int seriesCount = thisChart.Series.Count - 1;            //设置图表的显示样式
                thisChart.Series[seriesCount].ChartType = SeriesChartType.Line;
                thisChart.Series[seriesCount].BorderWidth = 4;            thisChart.Legends.Add(staid);
                Legend chartLegend = thisChart.Legends[seriesCount];
                //chartLegend.Position.Auto = false;
                chartLegend.Title = "水文站";            return thisChart.Series[seriesCount];
            }
      

  7.   


    这不是Excel里边的,是MS Chart 前身是Dundas Chart第三方控件 图表空间画各种各样的曲线柱形图之类的。
      

  8.   

    你看我贴的图,晕,不能上传本地图片,没办法了,要回家吃饭了,反正我的是Microsoft.Office.Interop.Excel 11.0的类库,图左边是查询Series,跟chart有关系的都没有.Series方法啊,右边是Excel.Series的所有方法和属性,也没有那个.Add()方法啊。
      

  9.   


    现在是Series属性为空,所以调用什么方法都会跑出同样的异常么。
      

  10.   

    我明白了,这个就是创建数据系列的,你前面是不是没有创建啊,就是说你这个chart创建以后是没有数据系列的,你现在想创建是吗?你没有创建Series哪来的去this.chart1.Series里查找有某个名字Serie啊,你看
    private Series AddChartSeries(string staid, string sDate, string dataType, string unit, Chart thisChart)
            {
    string staid参数就是数据系列名字啊,但是前提是得有Series。
      

  11.   

    因为Series是一个SeriesCollection对象,所以按道理只要chart1是一个正确的对象应该就会有这个Series对象了吧。我看了下动态创建图表的代码,没有看到需要new这么个东西,给你看下希望对你有用
    private void AddChartIntoPage(string staid, string dataType, string sDate, Panel dsPnl, Dictionary<string, double> dataList)
            {
                Chart newChart = new Chart();
                newChart.EnableViewState = false;
                newChart.Compression = 50;
                newChart.ImageType = ChartImageType.Jpeg;
                newChart.SuppressExceptions = true;
                newChart.Width = new Unit(chart.Width.Value + 100d);
                newChart.Height = chart.Height;            AddChartArea(ref newChart);            DataRow drDataType = (from q in dsDataType.Tables[0].AsEnumerable()
                                      where q.Field<string>("DataCode") == dataType
                                      select q).First();
                Series series = AddChartSeries(staid, sDate, drDataType["TypeName"].ToString(), drDataType["Unit"].ToString(), newChart);
                DataRow drStation = (from c in dsStation.Tables[0].AsEnumerable()
                                     where c.Field<string>("StationName") == staid
                                     select c).First();            AddOtherDataPoints(drStation["StationID"].ToString(), drDataType["TypeName"].ToString(), sDate, series, dataList);            dsPnl.Controls.Add(newChart);
            }private void AddChartArea(ref Chart thisChart)
            {
                ChartArea chartArea = new ChartArea();
                Axis axisX = new Axis(chartArea, AxisName.X);
                Axis axisY = new Axis(chartArea, AxisName.Y);
                SetAxisAttributes(ref axisX);
                thisChart.ChartAreas.Add(chartArea);
            }
      

  12.   


      public bool AddStyledSerie(string strSerieName, SeriesChartType sctType, Color color, int iWidth)
            {
                try
                {
                    if (this.chart1.Series.IndexOf(strSerieName) < 0)
                    {
                        this.chart1.Series.Add(strSerieName);
                        this.chart1.Series[strSerieName].ChartType = sctType;
                        this.chart1.Series[strSerieName].Color = color;
                        this.chart1.Series[strSerieName].BorderWidth = iWidth;
                        return true;
                    }
                }
                catch (System.NullReferenceException ex)
                {
                    string strMsg = ex.Message;
                }            return false;
            }
      

  13.   


     public bool AddStyledSerie(string strSerieName, SeriesChartType sctType, Color color, int iWidth)
      {
      try
      {
      if (chart1 == null) { MessageBox.Show("1"); return; }//按照理解这个应该不会执行
      else if (chart1.Series == null) { MessageBox.Show("2"); return; }//按照理解这个应该会执行?那这样就说明我们构造这个chart的方法不对吧
      if (this.chart1.Series.IndexOf(strSerieName) < 0)
      {
      this.chart1.Series.Add(strSerieName);
      this.chart1.Series[strSerieName].ChartType = sctType;
      this.chart1.Series[strSerieName].Color = color;
      this.chart1.Series[strSerieName].BorderWidth = iWidth;
      return true;
      }
      }
      catch (System.NullReferenceException ex)
      {
      string strMsg = ex.Message;
      }  return false;
      }
      

  14.   

    Chart构造过程应该没什么问题吧
    我的程序是在Chart基础上写的一个用户控件myChart,再其他程序里都没问题,唯独这个出现了问题
    程序运行的时候还可以运行几秒钟,时间长了就要出现这个错误。
    我的程序是多线程的,myChart再MDI的子窗体上,myChart1.AddPointIntoSerie(name, ……)也是在线程里调用的
      

  15.   


    Chart构造过程应该没什么问题吧
    我的程序是在Chart基础上写的一个用户控件myChart,再其他程序里都没问题,唯独这个出现了问题
    程序运行的时候还可以运行几秒钟,时间长了就要出现这个错误。
    我的程序是多线程的,myChart再MDI的子窗体上,myChart1.AddPointIntoSerie(name, ……)也是在线程里调用的
      

  16.   

    更新子窗体线程函数
            private static void ChildFormUpdateFunc()
            {
                while (true)
                {
                    int iChanelValueRecvedCount = 0;
                    Monitor.Enter(_Sync);
                    try
                    {
                        iChanelValueRecvedCount = ChanelValueRecved.Count;
                    }
                    finally
                    {
                        Monitor.Exit(_Sync);
                    }                if (iChanelValueRecvedCount > 0)
                    {
                        try
                        {
                            int sChannel = 0;
                            double[] dSensorValues = null;
                            double dTime = 0;
                            Monitor.Enter(_Sync);
                            try
                            {
                                sChannel = ChanelValueRecved.GetFirstChanel();
                                dSensorValues = ChanelValueRecved.GetFirstValues();
                                dTime = ChanelValueRecved.GetFirstSensorValueTime();                            ChanelValueRecved.Remove();
                            }
                            finally
                            {
                                Monitor.Exit(_Sync);
                            }
                            try
                            {                                        ((ChartForm)sortedListChannelToChart[ChanelSensorKeyValuePair.Key]).ChartDisplay(sChannel, dSensorValues, dTime, MDIParentDISLab.dAdjustECG, MDIParentDISLab.coefOfECG);                        }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message.ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            string str = ex.ToString();
                        }
                    }
                    else
                    {
                        Thread.Sleep(10);
                    }
                }
            }
      

  17.   

    我真的觉得这个不可能是Series为空而应该是chart1为空。你能不能提供一个证明,使得当Series为空时Chart1不为空?例如有如下形式
    [TestMethod()]
    public void NullObjectTest()
    {
      Chart chart = chart1;
      Assert.IsNotNull(chart);
      Assert.IsNull(chart.Series);
    }