1

別の例外を実際に覆っているXAMLParseExceptionが発生しています。スタックトレースの一部は次のとおりです。29 行 目は次のとおりです。
Message=Object reference not set to an instance of an object.
Source=AssignmentOrganizer
StackTrace:
at AssignmentOrganizer.MainWindow..ctor() in C:\Users\Mohit\Documents\Visual Studio 2010 \Projects\AssignmentOrganizer\AssignmentOrganizer\MainWindow.xaml.cs:line 29

lvwMain.ItemsSource = _assignmentRepo.ListAssignments();

ここで、lvwMainはListViewであり、_assignmentsRepoは次のように宣言されたIAssignmentRepositoryです。

IAssignmentRepository _assignmentRepo; 

ここでエラーが発生します。私はリポジトリパターンを使用しています。推測しても構わないと思っている人はいますか?
これが私のXAMLです:

<Window x:Class="AssignmentOrganizer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="MainWindow" Height="518" Width="755">
<DockPanel>
    <Menu DockPanel.Dock="Top">

    </Menu>
    <ToolBar DockPanel.Dock="Top">

    </ToolBar>
    <StatusBar DockPanel.Dock="Bottom">

    </StatusBar>
    <Grid DockPanel.Dock="Left" Width="150">
        <Grid.RowDefinitions>
            <RowDefinition Height="259*" />
            <RowDefinition Height="259*" />
        </Grid.RowDefinitions>
    </Grid>
    <Grid DockPanel.Dock="Right" Width="150">

    </Grid>
    <Grid>
        <ListView x:Name="lvwMain">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Title"  Width="125" />
                    <GridViewColumn Header="Due"  Width="75" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</DockPanel>

4

4 に答える 4

3

InitializeComponentコンストラクターでは、他のコンストラクターロジックを実行する前に、必ず呼び出しを行ってください。

public MainWindow()
{
    // Do this first.
    InitializeComponent();

    // Now do the rest of the constructor.
    ...
    lvwMain.ItemsSource = _assignmentRepo.ListAssignments();
    ...
}
于 2010-01-24T23:36:44.553 に答える
0

あなたがそれに割り当てたことがないからのよう_assignmentRepoに見えます。nullこの線

IAssignmentRepository _assignmentRepo; 

_assignmentRepo実装するオブジェクトへの参照である変数を宣言しますIAssignmentRepositoryが、実際にはそのようなオブジェクトをインスタンス化しません。コードのある時点で、次のような行が必要になります

_assignmentRepo = new AssignmentRepository();

ここで、AssignmentRepositoryはを実装するクラスですIAssignmentRepository。もちろん、1行で宣言してインスタンス化できます。

IAssignmentRepository _assignmentRepo = new AssignmentRepository();

次のような他のオプションがあります

_assignmentRepo = RepositoryFactory.CreateRepository<AssignmentRepository>();

これを確認する非常に簡単な方法は、問題のある行にブレークポイントを設定し、デバッガーを起動して、ブレークポイントに到達するまで実行してから、マウスをに置くことです_assignmentRepo。次に、小さなツールチップが表示され、_assignmentRepo実際にそうであるかどうかを確認できますnull

詳細を省略していて、実際に確実に割り当てられている_assignmentRepo場合、他の唯一の可能性lvmMainはnullです。なぜそうなるのかを推測するのに十分な情報を私たちに提供していません。

于 2010-01-24T00:55:40.920 に答える
0

問題はメソッド「ListAssignments()」にあると思います。このメソッドによって返されるコレクション内のアイテムの一部はnullであり、コントロールがすべてのアイテムをバインドしようとすると(すべてがnull以外であると予想されます)、nullオブジェクトに対して例外がスローされます。

これを試して...

lvwMain.ItemsSource = _assignmentRepo.ListAssignments().where(item=>item!=null).ToList();

理想的には、ListAssignments()はnullオブジェクトを無視する必要があります。ただし、これを試して例外を取り除くことができます。

于 2010-01-24T10:18:12.423 に答える
0

また、lvwMainがnullの場合、この例外が発生します。

于 2010-01-24T13:56:48.927 に答える