简体   繁体   中英

ObservableCollection.Add() calls a NullReferenceException

I have a strange dilemma that is making no sense. When I call properties.Add after a button click, the debugger shows a NullReferenceException .

The error is shown in the following constructor for MainWindow():

class GlobalProperties
{
    public ObservableCollection<Property> Properties { get; set; }
    public ObservableCollection<Photo> Photos { get; set; }
    public ObservableCollection<Problem> Issues { get; set; }
    public ObservableCollection<Tennant> Tennants { get; set; }
}

public partial class MainWindow : Window
{
    //...
    GlobalProperties globalProperties = new GlobalProperties();

    public MainWindow(User curUser)
    {
        ObservableCollection<Property> properties = globalProperties.Properties;
        properties.Add(new Property("58 Huet St", "Nundah", "4012", 4000.00, new LinkedList<Tennant>(), 5, 4, 2, 0, "Beautiful house. In Nundah. It's pretty cool. And you should live there. This is an optional piece of text.", new LinkedList<Photo>(), "Home", 48.23, 86.74, "Humid", new LinkedList<Problem>(), "Townhouse", 2004, 1800)); // This line has the problem
        globalProperties.Properties = properties;
        InitializeComponent();
        this.DataContext = this;
    }

Why am I getting this NullReferenceException ? It was working perfectly before I set up some Binding things on the MainWindow, after which it started giving me this.

Edit: Found the StackTrace:

at VentureVisions.view.MainWindow..ctor(User curUser) in c:\Source\Stuff\CS IA\VentureVisions\VentureVisions\view\MainWindow.xaml.cs:line 36
at VentureVisions.view.Login.OnClosing(CancelEventArgs e) in c:\Source\Stuff\CS IA\VentureVisions\VentureVisions\view\Login.xaml.cs:line 42
at System.Windows.Window.WmClose()
at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

Based on the stack trace you posted, NullReferenceException is thrown in the constructor of MainWindow , in line 36 of MainWindow.xaml.cs to be exact.

Try checking that and if you're still stuck, post the code of that constructor as well, marking the problematic source code line.

EDIT:

Try adding the following constructor to GlobalProperties and check if that fixes the problem:

public GlobalProperties()
{
    Properties = new ObservableCollection<Property>();
    Photos = new ObservableCollection<Photo>();
    Issues = new ObservableCollection<Problem> ();
    Tennants = new ObservableCollection<Tennant>();
}

Try this

var window = new MainWindow();
Application.Current.MainWindow = window;
window.Show();
this.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM