0

I've encountered this problem during development, the problem the compiler gives, is that an object reference is not set, So it should be some kind of null pointer

 <Window x:Class="BattleShip.MVVM.Views.NewGameSettings"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:viewModels="clr-namespace:BattleShip.MVVM.ViewModels"
            mc:Ignorable="d"
            Title="New Game" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
        <Window.DataContext>
            <viewModels:SettingsViewModel/>
        </Window.DataContext><Grid>
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="80" Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition MinHeight="30" Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="80" Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="80" Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Image Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Source="{StaticResource Background}" Height="120" Stretch="Fill"/>
    <Label
        x:Name="HeightLabel"
        Content="Height"
        Grid.Column="1" Grid.Row="1"
        Margin="0,10,0,0"/>
    <Label
        x:Name="WidthLabel"
        Content="Width"
        Grid.Column="1" Grid.Row="2"
        Margin="0,10,0,0"/>
    <Label
        x:Name="DifficultyLabel"
        Content="AI Difficulty"
        Grid.Column="1" Grid.Row="3"
        Margin="0,10,0,0"/>
    <TextBox x:Name="LengthBox" 
             TextWrapping="NoWrap"
             VerticalContentAlignment="Center"
             IsInactiveSelectionHighlightEnabled="True" 
             Grid.Column="2" 
             Grid.Row="1" 
             HorizontalAlignment="Left" 
             Width="100"
             Text="{Binding Height}"
             Margin="0,10,0,0"/>
    <TextBox x:Name="HeightBox" 
             TextWrapping="NoWrap" 
             VerticalContentAlignment="Center"
             Text="{Binding Width}" 
             Grid.Column="2" Grid.Row="2" 
             HorizontalAlignment="Left" 
             Width="100"
             Margin="0,10,0,0"/>
    <ComboBox ItemsSource="{Binding Difficulties}"
              SelectedItem="{Binding SelectedDifficulty}"
              Grid.Column="2" Grid.Row="3" 
              VerticalContentAlignment="Center"
              HorizontalAlignment="Left" 
              Width="100"
              Margin="0,10,0,0"/>

    <Button Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" Content="Start" Margin="0,10,0,0" Command="{Binding StartGameCommand}"/>
</Grid>

This is the code where it gives the error (line of ). How can I solve this problem since I initialized this view model?

Herm L
  • 37
  • 1
  • 8
  • 6
    Most probably it's a nullreference thrown by `viewModels:SettingsViewModel` constructor – quetzalcoatl Apr 19 '16 at 11:14
  • That's a good point. If you're dependent upon constructor parameters then you could programmatically set the `Window`'s `DataContext` in its code-behind file. – James Wright Apr 19 '16 at 11:15
  • [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – default Apr 19 '16 at 11:18

1 Answers1

-1

If you have correct ViewModel, the compiler error may be caused by Visual Studio's bug, happened in VS 2015,VS2013, etc.So the solution is :

Try to delete .suo file and then restart Visual Studio.

John Zhu
  • 1,009
  • 11
  • 11