views:

289

answers:

4

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>

A: 

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.

Jason
I tried assigning it to AssignmentRepository. But _assignmentRepo was not null as I learned from the breakpoint.
Mohit Deshpande
If I remove the line of code that sets the listview's item's source, the code runs just fine.
Mohit Deshpande
@Mohit Deshpande: If you're certain that you've assigned to `_assignmentRepo` then you need to also check `lvmMain`.
Jason
@Mohit Deshpande: "If I remove the line of code that sets the listview's item's source, the code runs just fine." Then that says exactly that `lvmMain` is null.
Jason
I posted my XAML
Mohit Deshpande
Did you edit the autogenerated code?
Jason
A: 

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.

Amby
The stack trace does not show the exception as being thrown inside `ListAssignments`.
Jason
It won't. Its perfectly fine for a method to return a collection with some null items in it. So no problem in ListAssignments().The exception is thrown when that collection is assigned to ItemSource, since it will iterate on each item (expecting all to be non-null) and create ListItem for each.
Amby
@Amby: Then it's misleading to say that the problem is with `ListAssignments`.
Jason
A: 

You would also get this exception is lvwMain is null.

Bobby
+2  A: 

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();
    ...
}
Cameron MacFarland