Initialize Component抛出空引用异常

时间:2011-06-21 11:02:15

标签: c# wpf

我在大型程序中有一个菜单项,可以打开一个新窗口。没有任何内容传递到它,它加载日历,3个空文本框,3个标签,2个按钮和一个空的Crystal Report Viewer。

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

这在我的系统上工作正常,但是当在同事系统上部署时,Initialize Component会抛出空引用异常。我无法在我的系统上重新创建例外。

有没有人知道我应该从哪里开始?

更新

ERROR

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>

由于

4 个答案:

答案 0 :(得分:6)

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

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

转到Debug -> Exceptions 然后检查所有方框。

答案 1 :(得分:3)

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

答案 2 :(得分:1)

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

在这两种情况下,都没有内部异常,也没有任何代码或任何后续代码可以进行调试。

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

请参阅以下示例(真实情况):

  • 我从Windows窗体迁移了一个应用程序。自从我使用Chart以来,我不得不继续使用一些System.Windows.Forms组件及其原理。
  • 我使用DataContext为显示图表的面板设置了MyChartPanel.DataContext = ChartControllerInstance
  • 到目前为止,程序运行良好,绑定一切正常
  • 后来,我使用XAML方式向DataContext添加Window来尝试建立视图模型。
  • 然后错误开始了。

发生了什么事?

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

所有这些图表绑定(在第一个版本中完全没有上下文)现在具有上下文:窗口视图模型,该图表不需要任何东西。

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

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

我知道这是一个糟糕的坏习惯,但是我还是WPF的新手,还没有找到更好的方法。

答案 3 :(得分:0)

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