c#error对象引用

时间:2016-06-02 14:46:04

标签: c# .net wpf visual-studio xaml

我有两个类和一个WPF窗口。这两个类是ShellViewModel.cs和UnitStatusTab.cs,WPF窗口叫做Shell.xaml。 ShellViewModel.cs是初始化的大多数变量的地方,因此它们可以在其他类中使用,也可以在整个项目中使用。 Shell.xaml有一个datagrid并显示大量数据。要从文件中检索该数据,使用的是UnitStatusTab.cs但由于某种原因我收到了错误

  

NullReferenceException未处理

这是ShellViewModel.cs中的一些代码

id

这是我在UnitStatusTab.cs中的代码

public class ShellViewModel : INotifyPropertyChanged
{

    Shell sh;
    readonly Shell shell;
    string PROBE_FOLDER = Config.GetDir(1);
    string RAC_FOLDER = Config.GetDir(2);
    string MMU_FOLDER1 = Config.GetDir(3);
    string MMU_FOLDER2 = Config.GetDir(4);

    public string User = "";
    public string Pass = "";
    public Thread initialOpen;
    public Thread InputThread;
    public Thread RACInputThread;
    public Thread MMUThread;
    public Thread ProbeThread;
    public string unitStatusBaseFileName = @"unit_status_";
    public string loadForecastBaseFileName = @"submitted_virtual_bids_";
    public DateTime inputdate;
    public bool Is_HDIC = true;


    public ShellViewModel(Shell shell)
    {
        this.shell = shell;
        IsIdle = true;
        IsSchedRateCalcIdle = true;
        ObsLog = new ObservableCollection<LogItem>();
        ObsProbeLog = new ObservableCollection<LogItem>();

        //obsDate = (DateTime.Now.Date).AddDays(1.0); // Commented this out so that current day date will be listed
        obsDate = DateTime.Now.Date;
        ObsRawDate = DateTime.Now.AddDays(-2);

        obsScen = "1";
        ObsProbeFolder = PROBE_FOLDER;

        ObsInputFolder = Path.Combine(Path.Combine(RAC_FOLDER, ObsDate.ToString("yyyyMMdd")),Path.Combine( "AC2", "Probe Input_CTO"));
        ObsInputFolderRAC = Path.Combine(Path.Combine(RAC_FOLDER, inputdate.ToString("yyyyMMdd")), Path.Combine("AC2", "Probe Input_t"));
        ObsOutputFolder = Path.Combine(Path.Combine(RAC_FOLDER, ObsDate.ToString("yyyyMMdd")), Path.Combine("AC2", "Probe Output_CTO"));
        ProbeXmlFactory factory = new ProbeXmlFactory();

        //ObsProbeOptions = new ObservableCollection<ProbeOption>(factory.CreateProbeOptions(Path.Combine(PROBE_FOLDER, "ProbeRAC_AM.xml")));

        ProbeInputEngine.Current.LogEvent += s => Log(s);
    }
}

这是我收到错误的行:

public class UnitStatusTab
{
    ShellViewModel shellVM;
    Shell shell;

    public void GetUnitStatusData()
    {
        this.shellVM = shellVM;
        for (int i = 1; i <= 24; i++)
        {
            obsStartHours.Add(i);
            obsEndHours.Add(i);
        }
        ObsUnitStatuses = new List<string>() { "A", "P", "M", "U", "*" };
        ObsStartHour = 1;
        ObsEndHour = 1;
        ObsSelectedStatus = "A";
        //ObsSelectedUnit = ObsUnits.FirstOrDefault();

        string filePath;

        filePath = System.IO.Path.Combine(shellVM.ObsInputFolder, @"unit_status_" + shellVM.ObsDate.ToString("yyyyMMdd") + ".csv");
        shellVM.unitStatusBaseFileName = @"unit_status_";
}

我尝试将filePath = System.IO.Path.Combine(shellVM.ObsInputFolder, @"unit_status_" + shellVM.ObsDate.ToString("yyyyMMdd") + ".csv"); 替换为ShellViewModel shellVM;来修复它,但那会给我一个错误

  

'ShellViewModel'不包含带0参数的构造函数

1 个答案:

答案 0 :(得分:-1)

它只是意味着您传递给Combine方法的值之一为null。在该行放置一个断点并查看哪个值为null然后继续并修复该类,使其不返回空值。如果您不知道问题是什么,盲目改变ViewModels不会解决问题。如果您需要更多帮助,则必须发布代码ShellViewModel。