[英]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.