繁体   English   中英

初始化组件抛出空引用异常

[英]Initialize Component throws a null reference exception

我在一个打开一个新窗口的大程序中有一个菜单项。 什么都没有传入,它加载了一个日历、3 个空文本框、3 个标签、2 个按钮和一个空的 Crystal Report Viewer。

当它运行时,用户单击日历,这会自动将所选月份的第一个和最后一个日期插入到 2 个文本框中。 一个按钮加载包含来自数据库的数据的 CR 报告,另一个按钮打印报告。

这在我的系统上运行良好,但初始化组件在部署在同事系统上时会引发空引用异常。 我无法在我的系统上重新创建异常。

有人对我应该从哪里开始有任何想法吗?

更新

错误

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Baml2006.Baml2006Reader.Process_PropertyWithConverter()
   at System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord()
   at System.Windows.Baml2006.Baml2006Reader.Process_BamlRecords()
   at System.Windows.Baml2006.Baml2006Reader.Read()
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at KeyInScreen.RebatesReports.InitializeComponent()
   at KeyInScreen.RebatesReports..ctor()
   at KeyInScreen.Menu.FertiliserRebate_Click(Object sender, RoutedEventArgs e)

XAML 代码

<Window x:Class="KeyInScreen.RebatesReports"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Fertiliser Rebates" WindowState="Maximized" 
    xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer" 
    mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="700" 
    Width="1350">
<Window.Resources>
    <Style TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="14" />
    </Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="FontSize" Value="14"/>
    </Style>
    <Style TargetType="{x:Type Calendar}">
        <Setter Property="FontSize" Value="14"/>
    </Style>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="FontSize" Value="14"/>
    </Style>
</Window.Resources>

<Grid Background="#FFEFEDDF">
    <Grid.RowDefinitions>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height="4*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="5*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".2*"/>
        <ColumnDefinition Width=".7*"/>
        <ColumnDefinition Width=".7*"/>
        <ColumnDefinition Width=".7*"/>
        <ColumnDefinition Width=".5*"/>
        <ColumnDefinition Width="4*"/>
        <ColumnDefinition Width=".1*"/>
    </Grid.ColumnDefinitions>
    <Calendar x:Name="cDatePicker" Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" Grid.ColumnSpan="2" SelectedDatesChanged="cDatePicker_SelectedDatesChanged" />
    <Label Content="Start Date:" Grid.Column="1" Grid.Row="4" Margin="5"/>
    <TextBox x:Name="txtStartDate" Grid.Column="2" Grid.Row="4" Margin="5" VerticalContentAlignment="Center" />
    <Label Content="End Date:" Grid.Column="1" Grid.Row="5" Margin="5"/>
    <TextBox x:Name="txtEndDate" Grid.Column="2" Grid.Row="5" Margin="5" VerticalContentAlignment="Center"/>
    <Label Content="Supplier Number:" Grid.Column="1" Grid.Row="6" Margin="5"/>
    <TextBox x:Name="txtCustomerNumber" Grid.Column="2" Grid.Row="6" Margin="5" VerticalContentAlignment="Center" TabIndex="1" />
    <Button x:Name="btnShowRport" Content="View Report" Grid.Column="1" Grid.Row="8" Margin="0,0,10,0"  Click="btnShowRport_Click" IsDefault="True" />
    <Button x:Name="btnPrintReport" Content=" Print Report" Grid.Column="2" Grid.Row="8" Margin="10,0,0,0"  Click="btnPrintReport_Click" />
    <my:CrystalReportsViewer  x:Name="crReportViewer" Grid.Column="3" Grid.RowSpan="10" HorizontalAlignment="Center" VerticalAlignment="Center" 
                              Height="656" Width="845" Margin="94,-7,62,12" ToggleSidePanel="None" Panel.ZIndex="0" Grid.ColumnSpan="3" 
                              ShowPrintButton="False" ShowRefreshButton="False" ShowSearchTextButton="False" ShowToggleSidePanelButton="True"
                              ShowToolbar="True" ShowNextPageButton="True" ShowGoToPageButton="True" ShowExportButton="False" 
                              ShowCopyButton="False" SnapsToDevicePixels="True" ShowLogo="False" ShowPrevPageButton="True" 
                              ShowStatusbar="True" />
</Grid>

谢谢

有点晚了,但是当你得到这样的空引用异常时,它可能会产生误导。 内部异常可能为空(在我的情况下。)

我发现这个问题的方法是在 VS 2010 中启用第一次机会异常。

转到Debug -> Exceptions ,然后选中所有框。

您需要查看内部异常。 当 initialcomponent 被调用时,它可以触发其他必须导致异常的代码。 内部异常应该为您提供所需的所有信息。 请注意,内部异常本身可能具有内部异常。 修改您的应用程序以将所有这些写入文件或剪贴板或其他东西。

我两次遇到这个问题,在这两种情况下,我都缺乏对 WPF 编程有益的模式知识。

在这两种情况下,都没有内部异常,也没有代码或任何需要遵循和调试的东西。

碰巧有一个Binding指向DataContext对象中不存在的Path 很难找到问题,但我建议去寻找你拥有的每个Binding和各自的数据上下文,记住你可能有一个来自上游父级的上下文。

看这个例子(真实案例):

  • 我从 Windows 窗体迁移了一个应用程序。 由于我使用的是Chart ,因此我不得不继续使用一些System.Windows.Forms组件及其理念。
  • 我使用MyChartPanel.DataContext = ChartControllerInstance以编程方式为显示图表的面板设置DataContext
  • 到目前为止一切顺利,程序运行良好,绑定一切正常
  • 后来,我使用XAML的方式将DataContext添加到Window ,以尝试拥有一个视图模型。
  • 然后bug开始了。

发生了什么?

由于图表数据上下文未在 XAML 中设置,而仅通过代码设置,因此窗口数据上下文被传播到其所有子项。

所有这些图表绑定(在第一个版本中根本没有上下文)现在都有一个上下文:窗口视图模型,它没有图表所需的任何东西。

出现空引用是因为那些绑定找到了上下文,但没有找到它们在上下文中的路径。

解决方案? 我必须在图表面板中显式使用DataContext={Binding null} ,以防止其绑定在窗口上下文中搜索。 这不会引发异常,并且稍后在代码中以编程方式设置图表上下文,并且一切正常。

我知道这是一种非常糟糕的做法,但我仍然是 WPF 的新手,还没有找到更好的方法。

当我在 GAC(C:\Windows\Microsoft.NET\assembly\GAC_MSIL => GAC location for .NET framework 4.0 或更高版本)位置重命名/删除旧程序集时,问题消失了。

如果您不调用InitializeComponent();可能会发生这种情况。 在构造函数内

例如

public partial class LogExaminer : Window
{
    public LogExaminer()
    {
        InitializeComponent();    //comment out this to see the error
    }
}

在 Xamarin 中,您可以启用编译绑定。 在这种情况下,编译器将捕获绑定问题。

考虑为您的项目启用此功能,这样可以节省时间。 我看到了一些 WPF 插件。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM