0

I'm opening an object with series and a date. I can use all the series but in the last line "_measurement.MeasurementInfo.StartDateAndTime = seriesObject.Date;" I get an error that says "Object reference not set to an instance of an object". I looked on the internet and some guys said the object would be null - but the object isn't null. The object is seriesObject = {BSIAnalyzer.SeriesObject}. Can somebody help me with this?

    private void openAnalysis()
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "BSI analyses | *.anl";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            SeriesObject seriesObject = null;
            try
            {
                FileManager fileManager = new FileManager();
                seriesObject = fileManager.Load<SeriesObject>(ofd.FileName);
            }
            catch (Exception e)
            {
                throw e;
            }
            // TODO same loop for scatter
            if (seriesObject.LineSeries != null)
            {
                foreach (LineSeriesObject lineSeriesObject in seriesObject.LineSeries)
                {
                    if (lineSeriesObject.Name == "_sBSIV1SeriesA")
                    {
                        if (lineSeriesObject.DataPoints != null)
                        {
                            foreach (LineSeriesObjectDataPoint dataPoint in lineSeriesObject.DataPoints)
                            {
                                _sBSIV1SeriesA.Points.Add(new DataPoint(dataPoint.X, dataPoint.Y));
                            }
                        }
                        this.plotBSITotalA.Model.Series.Add(_sBSIV1SeriesA);
                    }
                    **For all series in object**
                    refreshPlots(true);
                }
            }
            if (seriesObject.ScatterSeries != null)
            {
                foreach (ScatterSeriesObject scatterSeriesObject in seriesObject.ScatterSeries)
                {
                    if (scatterSeriesObject.Name == "_sBSIV1ArtifactSeriesA")
                    {
                        if (scatterSeriesObject.DataPoints != null)
                        {
                            foreach (ScatterSeriesObjectDataPoint dataPoint in scatterSeriesObject.DataPoints)
                            {
                                _sBSIV1ArtifactSeriesA.Points.Add(new DataPoint(dataPoint.X, dataPoint.Y));
                            }
                        }
                        this.plotBSITotalA.Model.Series.Add(_sBSIV1ArtifactSeriesA);
                    }
                    **For all series in object** 
                }
            }
            refreshPlots(true);   

            _measurement.MeasurementInfo.StartDateAndTime = seriesObject.Date;           
        }
    }

The clas from the object is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace BSIAnalyzer
{ 
[Serializable]
public class LineSeriesObjectDataPoint
{
    public Double X { get; set; }
    public Double Y { get; set; }
    public LineSeriesObjectDataPoint(Double x, Double y)
    {
        this.X = x;
        this.Y = y;
    }
}
[Serializable]
public class LineSeriesObject
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public List<LineSeriesObjectDataPoint> DataPoints { get; set; }

    public LineSeriesObject()
    {
        this.InitMembers();
    }

    private void InitMembers()
    {
        this.DataPoints = new List<LineSeriesObjectDataPoint>();
    }
}
[Serializable]
public class ScatterSeriesObjectDataPoint
{
    public Double X { get; set; }
    public Double Y { get; set; }

    public ScatterSeriesObjectDataPoint(Double x, Double y)
    {
        this.X = x;
        this.Y = y;
    }
}
[Serializable]
public class ScatterSeriesObject
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public List<ScatterSeriesObjectDataPoint> DataPoints { get; set; }

    public ScatterSeriesObject()
    {
        this.InitMembers();
    }

    public void InitMembers()
    {
        this.DataPoints = new List<ScatterSeriesObjectDataPoint>();
    }
}
[Serializable]
class SeriesObject
{
    public String Date { get; set; }
    public List<LineSeriesObject> LineSeries { get; set; }
    public List<ScatterSeriesObject> ScatterSeries { get; set; }
    public SeriesObject()
    {
        this.InitMembers();
    }
    private void InitMembers()
    {
        this.LineSeries = new List<LineSeriesObject>();
        this.ScatterSeries = new List<ScatterSeriesObject>();
    }
}
class FileManager
{
    public FileManager()
    {
        this.InitMembers();
    }
    private void InitMembers()
    {
    }
    public T Load<T>(String path)
    {
        try
        {
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            T myLoadedObject = (T)binaryFormatter.Deserialize(fileStream);
            fileStream.Close();
            return myLoadedObject;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

}

4

1 回答 1

3

If the exception is thrown on this line:

_measurement.MeasurementInfo.StartDateAndTime = seriesObject.Date;

Then one of the following is null:

seriesObject 
_measurement
_measurement.MeasurementInfo

Without seeing the initialization of _measurement or MeasurementInfo it's impossible to know why one of them is null just from looking at the code.

于 2013-10-31T12:39:30.067 回答