简体   繁体   中英

NullReferenceException when accessing static ObservableCollection

I'm working on a program for my Windows Phone class and I hit a bit of a problem. When I try to fire up the application, I get a null reference exception when trying to access a static ObservableCollection. I thought that because it was static, I didn't need to instantiate it. Am I doing something wrong here? Here's the method:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    this.DataContext = null;
    this.DataContext = Settings.NotesList;
    Settings.CurrentNoteIndex = -1;
    TheListBox.SelectedIndex = -1;
    if (Settings.NotesList.Count <= 0) // EXCEPTION
    {
        NoteStatus.Visibility = System.Windows.Visibility.Visible;
        TheListBox.Visibility = System.Windows.Visibility.Collapsed;
    }
    else
    {
        NoteStatus.Visibility = System.Windows.Visibility.Collapsed;
        TheListBox.Visibility = System.Windows.Visibility.Visible;
    }
}

Where in a separate file I have:

public static class Settings
{
    static Settings() { }
    public static ObservableCollection<Note> NotesList;
    static IsolatedStorageSettings settings;
    private static int currentNoteIndex;
    public static int CurrentNoteIndex { get; set; }
}

I wanted to test the program before writing any more but I'm not sure what's causing this. The OnNavigatedTo is from launching the application so I never even get to MainPage.xaml. Help is very appreciated.

即使通过静态,它仍然需要在某个地方实例化。

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