SelectedIndex是一个空引用异常吗?

3

我一直收到这个错误:

System.NullReferenceException was unhandled by user code
  Message=[Arg_NullReferenceException]
Arguments: 
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.60401.00&File=mscorlib.dll&Key=Arg_NullReferenceException
  StackTrace:
       at Jantire.DoHomeworkView.TextAlignment_combobox_SelectionChanged(Object sender, SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
       at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
       at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemCollection.NotifyCollectionReady()
       at System.Windows.Controls.ItemsControl.NotifyAllItemsAdded(IntPtr nativeItemsControl)
  InnerException: 

在代码中:

private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //This next line is where error is at
            if (TextAlignment_combobox.SelectedIndex == 0)
            {
                EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty,  TextAlignment.Left);
            }
            if (TextAlignment_combobox.SelectedIndex == 1)
            {
                EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
            }
            if (TextAlignment_combobox.SelectedIndex == 2)
            {
                EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
            }
        }

使用XAML:

<ComboBox Width="128" x:Name="TextAlignment_combobox" SelectionChanged="TextAlignment_combobox_SelectionChanged" ToolTipService.ToolTip="Text Alignment">
                <ComboBoxItem Name="LeftAlignment_comboboxitem" Content="Left Alignment" IsSelected="True"/>
                <ComboBoxItem Name="CenterAlignment_comboboxitem" Content="Center Alignment"/>
                <ComboBoxItem Name="RightAlignment_comboboxitem" Content="Right Alignment"/>
            </ComboBox>

如果您想得到答案,请提供更多细节。例如,在TextAlignment_combobox_SelectionChanged的哪一行发生了NullReferenceException?您提供的信息越详细,获得的答案就会越多。 - Peter Ritchie
我在代码中的错误行前加了注释以显示出错位置。EssayContents_richtextbox的内容为空,但是我该如何设置默认文本对齐方式? - Jarred Sumner
3个回答

5

好的,我已经测试了这种情况并找到了你的问题。当你最初启动WPF应用程序时,它会运行SelectionChanged事件。这发生在ComboBox对象创建之后。问题是,在你的WPF应用程序中,你的ComboBox在RichTextBox之前的XAML中。这意味着在RichTextBox创建之前就会触发此事件。因此,你会得到一个空引用异常。你有两个选择。你可以忽略错误或尝试确定RichTextBox是否存在,然后再尝试对其进行操作,或者将RichTextBox在XAML中上移以使其位于ComboBox之上。这与表单位置无关,而是与XAML中的位置有关。任何一种方法都可以解决你的问题。


1
解决了!我把它放在包含工具栏的StackPanel之前,但仍然不起作用,我猜测这是因为RichTextBox加载需要比按钮更长的时间,所以我只是在每个工具栏按钮之前加入了if (EssayContents_richtextbox != null)。 - Jarred Sumner

0

没有更多的细节,听起来像是EssayContents_richtextbox为空。


那似乎是问题所在,但我该如何设置默认文本对齐方式? - Jarred Sumner

0

可能是以下原因之一:

  • TextAlignment_combobox 为空(似乎不太可能)
  • EssayContents_richtextbox 为空
  • 或者 EssayContents_richtextbox.Selection 为空

您应该调试代码并检查它们,直到找到空值。如果无法解决问题,请在代码中进行保护,例如:

private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (EssayContents_richtextbox == null || EssayContents_richtextbox.Selection == null)
    {
        // Handle me, or just
        return;
    }

    if (TextAlignment_combobox.SelectedIndex == 0)
    {
        EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty,  TextAlignment.Left);
    }
    if (TextAlignment_combobox.SelectedIndex == 1)
    {
        EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
    }
    if (TextAlignment_combobox.SelectedIndex == 2)
    {
        EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
    }
}

我猜如果EssayContents_richtextbox为空,那么这是你代码中的错误 - 同时查看Selection属性的文档,即使没有选择,它似乎也会有一个值(尽管它没有明确说明它永远不会返回null)。

TextAlignment_combobox 不能为空,否则它将无法生成SelectionChanged事件。如果EssayContents_richtextbox不为空,则Selection不能为null,否则您将无法在RichTextBox上设置选择。 - Peter Ritchie
@Peter,那不是真的——根据发布的堆栈跟踪和XAML,这种情况非常不可能(这就是为什么我说“看起来非常不可能”),但绝对不是不可能的(可能是另一个控件触发了更改事件——我相当确定我可以设计出一个牵强附会的场景来实现这一点)。 - Justin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接