0

I have two classes and a WPF window. The two classes are ShellViewModel.cs and UnitStatusTab.cs and the WPF windows is called Shell.xaml. The ShellViewModel.cs is where most of the variables of the initialized so that they could be used in other classes and throughtout the project. Shell.xaml has a datagrid and displays a much of data. To retrieve that data from files the UnitStatusTab.cs is used but for some reason I'm getting the error

NullReferenceException was unhandled

This is some of the code in ShellViewModel.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);
    }
}

This is my code in UnitStatusTab.cs

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

This is the line where I'm getting the error on:

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

I've tried fixing it by replacing ShellViewModel shellVM; with ShellViewModel shellVM = new ShellViewModel(); but thats giving me another error

'ShellViewModel' does not contain a constructor that takes 0 arguments

newguy
  • 21
  • 6
  • What do the constructors for the `ShellVM` class look like? – Lee Jun 02 '16 at 14:48
  • From your code, you need a `Shell` instance to create a `ShellViewModel` e.g. `new ShellViewModel(shell)`. – Charles Mager Jun 02 '16 at 14:53
  • or you need to add the empty constructor public ShellViewModel() { } – Sergi0 Jun 02 '16 at 14:54
  • @CharlesMager I'm not understanding what you're saying. Could you please explain what you mean. Thanks – newguy Jun 02 '16 at 14:56
  • `this.shellVM = shellVM;` makes no sense! It's assigning `shellVM` to itself. Why did you write that? It's also what's probably "null". – MBender Jun 02 '16 at 14:58
  • I think you should got and read about [some basics](https://msdn.microsoft.com/en-gb/library/x9afc042.aspx). – Charles Mager Jun 02 '16 at 14:59
  • @Shaamaan I added ** this.shellVM = shellVM;** to see if it'll try to fix the error but obviously it doesn't. The problem is that shellVM is null and I don't know why – newguy Jun 02 '16 at 15:05
  • Well, the line is garbage. :P And `shellVM` is null because it's never assigned. Why? Because you never do it in your code. `UnitStatusTab.shellVM` is (implicitly) `private`, so you can't assign it from outside of the class, and you never assign it inside the class. – MBender Jun 02 '16 at 15:07
  • @Shaamaan How do I properly assign shellVM? – newguy Jun 02 '16 at 15:09
  • I have no idea. It's your code. Most likely you need to make `UnitStatusTab` require it as a parameter in the constructor (`public UnitStatusTab(ShellViewModel shellVM)` - in this case `this.shellVM = shellVM;` makes sense). But since you have not shown us how you use `UnitStatusTab` I don't know if anything else would be needed. – MBender Jun 02 '16 at 15:33

1 Answers1

-1

It just means that one of the values you're passing to the Combine method is null. Put a break point at the line and see which value is null and then go ahead and fix the class so it doesn't return a null value. Blindly changing ViewModels isn't going to fix the issue if you don't know what the issue is. If you'd like more help you'll have to post the code ShellViewModel.

Peter4499
  • 685
  • 7
  • 15