Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectedIndex是否为空引用异常?_C#_Visual Studio 2010_Silverlight 4.0_Nullreferenceexception - Fatal编程技术网

C# SelectedIndex是否为空引用异常?

C# SelectedIndex是否为空引用异常?,c#,visual-studio-2010,silverlight-4.0,nullreferenceexception,C#,Visual Studio 2010,Silverlight 4.0,Nullreferenceexception,我不断地发现这个错误: 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. Se

我不断地发现这个错误:

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>

没有更多细节,听起来像是EssayContents\u richtextbox为空。

或者:

  • TextAlignment\u组合框
    为空(似乎不太可能)
  • EssayContents\u richtextbox
    为空
  • EssayContents\u richtextbox。所选内容
    为空
您应该对此进行调试并检查它们,直到您在代码中发现某个为null的内容,或者无法以某种方式防止它们为null,例如:

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\u richtextbox
为空,这是代码中的一个错误-同时查看
Selection
属性的文档,即使没有选择,它也可能有一个值(尽管它没有明确说明它永远不会返回空值)

好,我已经测试了这个场景,发现了您的问题。当您最初启动WPF应用程序时,它将运行SelectionChanged事件。一旦创建了ComboBox对象,就会发生这种情况。问题是,在WPF应用程序中,您的组合框位于RichTextBox之前的XAML中。这意味着此事件在创建RichTextBox之前激发。因此,您会得到一个空引用异常。你有两个选择。您可以在尝试对RichTextBox进行操作之前确认该错误,或者可以在XAML中将RichTextBox上移到ComboBox上方。这与表单放置无关,而是与XAML中的放置有关。任何一个都可以解决您的问题。

如果您需要答案,请提供更多详细信息。i、 e.发生NullReferenceException时,文本对齐\组合框\选择的哪一行发生了更改?你越容易回答这个问题,你得到的答案就越多。我在代码的哪一行前加了一条注释,说明了错误所在。EssayContents\u richtextbox的内容为空,但如何设置默认文本对齐方式?这似乎是个问题,但如何设置默认文本对齐方式?TextAlignment\u combobox不能为空,或者它不可能生成SelectionChanged事件。如果EssayContents\u richtextbox不为null,则选择不能为null,否则您将永远无法在RichTextBox@Peter这不是真的——考虑到堆栈跟踪和xaml的发布(这就是为什么我说“似乎不太可能”),这是非常不可能的,但这绝对不是不可能的(它可能是触发更改事件的另一个控件-我很确定我可以设计一个发生这种情况的人为场景)。这解决了它!我将它放在包含工具栏的StackPanel之前,但它仍然不起作用,我猜是因为RichTextBox的加载时间比按钮长,所以我只包含了if(EssayContents\u richtextbox!=null)在每个工具栏按钮之前