我正在用系列和日期打开一个对象。我可以使用所有系列,但在最后一行“_measurement.MeasurementInfo.StartDateAndTime = seriesObject.Date;”我收到一个错误,上面写着“对象引用没有设置为对象的实例”。我在互联网上看了一些人说这个对象是空的。但该对象不为null。对象是seriesObject = {BSIAnalyzer.SeriesObject}。 (对不起我破碎的Englisch)。有人可以帮我吗?
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;
}
}
来自对象的clas是:
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;
}
}
}
}
答案 0 :(得分:2)
如果在此行引发异常:
_measurement.MeasurementInfo.StartDateAndTime = seriesObject.Date;
然后以下其中一项为空:
seriesObject
_measurement
_measurement.MeasurementInfo
如果没有看到_measurement
或MeasurementInfo
的初始化,就不可能知道为什么其中一个因查看代码而为空。