简体   繁体   English

访问静态ObservableCollection时发生NullReferenceException

[英]NullReferenceException when accessing static ObservableCollection

I'm working on a program for my Windows Phone class and I hit a bit of a problem. 我正在为Windows Phone类编写程序,但遇到了一些问题。 When I try to fire up the application, I get a null reference exception when trying to access a static ObservableCollection. 当我尝试启动应用程序时,尝试访问静态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. OnNavigatedTo是从启动应用程序开始的,所以我什至都无法进入MainPage.xaml。 Help is very appreciated. 非常感谢您的帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问某个对象时出现NullReferenceException - NullReferenceException when accessing some object foreach在ObservableCollection上引发NullReferenceException - foreach throws NullReferenceException on ObservableCollection 在构造函数外部访问属性时发生NullReferenceException - NullReferenceException when accessing a property outside of the constructor 访问EF导航属性时,请避免NullReferenceException - Avoid NullReferenceException when accessing EF Navigation Properties 是什么导致在访问DataGridView的单元格时出现此NullReferenceException? - What Causes this NullReferenceException when Accessing a Cell of a DataGridView? 访问列表时实体框架 NullReferenceException - Entity Framework NullReferenceException when accessing lists ObservableCollection.Add()调用NullReferenceException - ObservableCollection.Add() calls a NullReferenceException 将项目添加到ObservableCollection时发生NullReferenceException - NullReferenceException while adding item to ObservableCollection 仅访问模型时的ASP.NET Core NullReferenceException - ASP.NET Core NullReferenceException when just accessing model 访问DBSet时出现NullReferenceException。 使用MySQL,EF6 - NullReferenceException when accessing DBSet. Using MySQL, EF6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM