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

}

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
E. Verdi
  • 310
  • 2
  • 19
  • Try Debugging? Check that the object value is actually null, saying that it is set to something doesn't mean that it's not going to be null. – Philip Gullick Oct 31 '13 at 12:37
  • These are best solved using the debugger rather than static analysis. – D Stanley Oct 31 '13 at 12:38
  • Some guys told you what was going on. It's null. – gleng Oct 31 '13 at 12:39
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jehof Oct 31 '13 at 12:40
  • Wow some fast reaction. Already did debugging. That's why i found that the object was: seriesObject = {BSIAnalyzer.SeriesObject}. @D Stanley: _measurement seems to be null. But still don't know how to fix it – E. Verdi Oct 31 '13 at 12:41
  • @E.Verdi Set it to something other than null? `_measurement = new Measurement()`? Something like that should probably happen in the constructor. – D Stanley Oct 31 '13 at 12:48
  • It seems _measurement = new BrainRTMeasurement(fileName, _isOnlineContext); was standing outside the method. I feel like a noob. Thanks for the reaction :) – E. Verdi Oct 31 '13 at 12:51

1 Answers1

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you. It seems _measurement = new BrainRTMeasurement(fileName, _isOnlineContext); was standing outside the method. I feel like a noob. Thanks for the reaction :) – E. Verdi Oct 31 '13 at 12:47