开发者

object reference is not set to an instance of an object repository

I am getting an XAMLParseException that is really covering up another exception. Here is part of the stacktrace:

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

Here is line 29:

lvwMain.ItemsSource = _assignmentRepo.ListAssignments();

Where lvwMain is a ListView and _assignmentsRepo is an IAssignmentRepository declared like:

IAssignmentRepository _assignmentRepo; 

That is where the error occurs. I am using the repository pattern Anyone willing to take a guess?

Here is my 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>


In your constructor, make sure you put the InitializeComponent call before doing any other constructor logic.

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

    // Now do the rest of the constructor.
    ...
    lvwMain.ItemsSource = _assignmentRepo.ListAssignments();
    ...
}


I think the problem is with method "ListAssignments()". Some of the items in the collection returned by this method is null, and when control tries to bind all the items (expecting all to be NON null) it throws exception for a null object.

try this...

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

ideally, ListAssignments() should ignore null objects. But you can try this to get rid of the exception.


You would also get this exception is lvwMain is null.


Looks like _assignmentRepo is null because you never assigned to it. The line

IAssignmentRepository _assignmentRepo; 

declares a variable _assignmentRepo that is a reference to an object that implements IAssignmentRepository but it does not actually instantiate such an object. At some point in your code you need a line like

_assignmentRepo = new AssignmentRepository();

where AssignmentRepository is a class that implements IAssignmentRepository. Of course, you can declare and instantiate in one line:

IAssignmentRepository _assignmentRepo = new AssignmentRepository();

There are other options such as

_assignmentRepo = RepositoryFactory.CreateRepository<AssignmentRepository>();

A very simple way to check this is to set a breakpoint on the offending line, start up the debugger and run until you hit the breakpoint, and then hover the mouse over _assignmentRepo. Then a little tooltip will come up and you can see if _assignmentRepo is indeed null.

If you are omitting a detail and you have in fact definitely assigned _assignmentRepo then the only other possibility is that lvmMain is null. You haven't given us enough information to deduce why that could be the case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜