我试图创建一个保存数据库信息的类对象。不幸的是,它一直告诉我System.NullReferenceException并且我不明白为什么。
这是对象:
[Serializable]
public class DataBase
{
public int NumberOfFilesBackedUp { set; get; } = 0;
public class DataBaseItem // I tried struct too, but also no-go
{
public DateTime timeStamp { set; get; }
public string src { set; get; }
public string dst { set; get; }
public int type { set; get; }
public IntPtr pid { set; get; }
}
public List<DataBaseItem> BackupList { set; get; }
public DataBase()
{
BackupList = new List<DataBaseItem>();
}
}
在主应用程序(wpf)中:
public partial class App : Application
{
public static DataBase DBObj; // declaring the object
...
public App()
{
...
// Create/Load DB-Object
CreateLoadDB();
}
private void CreateLoadDB()
{
...
try
{
DBObj = Pickler.ReadFromBinaryFile<DataBase>(DBObjFile);
...
}
catch (FileNotFoundException)
{
DBObj = new DataBase(); // instantiated, in debugging the code always comes here, as I don't yet have a serialized save file for any past objects
}
private void BackupManager_BackupEvent(string SrcPath, string DstPath, int Deleted, IntPtr Pid)
{
...
try
{
var DBItem = new DataBase.DataBaseItem(){ timeStamp = timestamp, src = SrcPath, dst = DstPath, type = Deleted, pid = Pid};
DBObj.BackupList.Add(DBItem); // This throws the error...
}
DBobj未实例化。如果我在错误之前再次实例化它,那么没问题。但是我不想在那里实例化它,我需要在它当前被实例化的地方实例化它。
我不认为这是重复的,我知道Null Reference Exceptions是什么 - 我试图理解为什么我在这里得到它,因为我之前清楚地实例化了这个对象。