Anja Simonsen

Anja Simonsen

  • NA
  • 41
  • 15.4k

Object reference not set to an instance of an object.

Mar 15 2021 5:21 PM
I have used all day to figure out what happens, and why this errormessage is shown when I link to a view with data from db. But is fine, when only viewing some textstring.
 
My solution have 5 projects: TheUI, DAL, Model, ViewModel and Views. When run to curser method I can see that the 3 rows from the db goes fine into the Views (UserControls), but when linking on the UI to the specifc UserControl something goes wrong.
 
I'm pretty sure, there is nothing wrong with DAL and Model, so I'm showing you the 3 other projects.
 
I'm not cable to attach the file. I do not know why the solution zipped is sice 47mb. If You want code for the other projects, just say so.
 
The error is in below line in MainWindow.xaml line 110
  1. <views:CategoryView x:Name="CategoryView" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" />  
Best regards
Simsen :)
ViewModel.Account.CategoryViewModel:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Collections.ObjectModel;  
  5. using Model.Account;  
  6. using DAL.Account;  
  7. namespace ViewModel.Account  
  8. {  
  9. public class CategoryViewModel  
  10. {  
  11. private Category _selectedCategory;  
  12. public MyICommand DeleteCommand { getset; }  
  13. public Category SelectedCategory  
  14. {  
  15. get  
  16. {  
  17. return _selectedCategory;  
  18. }  
  19. set  
  20. {  
  21. _selectedCategory = value;  
  22. DeleteCommand.RaiseCanExecuteChanged();  
  23. }  
  24. }  
  25. public CategoryViewModel()  
  26. {  
  27. LoadCategories();  
  28. DeleteCommand = new MyICommand(OnDelete, CanDelete);  
  29. }  
  30. public ObservableCollection<Category> Categories { getset; }  
  31. public void LoadCategories()  
  32. {  
  33. DalCategory dalCategory = new DalCategory();  
  34. ObservableCollection<Category> dalCategories = dalCategory.GetCategories();  
  35. Categories = dalCategories;  
  36. }  
  37. #region Delete  
  38. private void OnDelete()  
  39. {  
  40. Categories.Remove(SelectedCategory);  
  41. }  
  42. private bool CanDelete()  
  43. {  
  44. return SelectedCategory != null;  
  45. }  
  46. #endregion  
  47. }  
  48. }
Views.CategoryView.Account.CategoryView.xaml:
  1. <UserControl x:Class="Views.Account.CategoryView"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6. xmlns:viewModel = "clr-namespace:ViewModel.Account;assembly=ViewModel"  
  7. xmlns:data = "clr-namespace:Model.Account;assembly=Model"  
  8. mc:Ignorable="d"  
  9. d:DesignHeight="450" d:DesignWidth="800">  
  10. <Grid>  
  11. <StackPanel Orientation = "Horizontal">  
  12. <ListBox ItemsSource = "{Binding Mode=OneWay}">  
  13. <DataTemplate>  
  14. <StackPanel Orientation="Horizontal">  
  15. <TextBlock Text="{Binding CategoryId}" />  
  16. <TextBlock Text="{Binding CategoryName}" />  
  17. <TextBlock Text="{Binding CategoryIsGlobal}" />  
  18. <TextBlock Text="{Binding ProjectId}" />  
  19. <TextBlock Text="{Binding IsObsolete}" />  
  20. </StackPanel>  
  21. </DataTemplate>  
  22. </ListBox>  
  23. </StackPanel>  
  24. </Grid>  
  25. </UserControl>  
Views.CategoryView.Account.CategoryView.xaml.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15. namespace Views.Account  
  16. {  
  17. /// <summary>  
  18. /// Interaction logic for CategoryView.xaml  
  19. /// </summary>  
  20. public partial class CategoryView : UserControl  
  21. {  
  22. public CategoryView()  
  23. {  
  24. InitializeComponent();  
  25. this.DataContext = new ViewModel.Account.CategoryViewModel();  
  26. string HereICheckTheDataContextAndItHas3Rows = "";  
  27. }  
  28. }  
  29. }  
AnsiBug.MainWindow.xaml:
  1. <Window xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Name="AnsiBug" x:Class="AnsiBug.MainWindow"  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6. xmlns:local="clr-namespace:AnsiBug"  
  7. xmlns:views = "clr-namespace:Views.Account;assembly=Views"  
  8. mc:Ignorable="d"  
  9. Title="MainWindow" Height="800" Width="1200" HorizontalAlignment="Center" VerticalAlignment="Center" WindowStartupLocation="CenterScreen">  
  10. <Window.Resources>  
  11. <!--#region Context Menus-->  
  12. <!--#region Reports-->  
  13. <ContextMenu x:Key="cmBtnReports" >  
  14. <MenuItem x:Name="BtnTextReports" Header="Tekst rapporter" />  
  15. <MenuItem x:Name="BtnReporsMyBugs" Header="Mine fejl" />  
  16. <MenuItem x:Name="BtnReporsNewBugs" Header="Nye fejl" />  
  17. <MenuItem x:Name="BtnReporsAllBugs" Header="Alle fejl" />  
  18. <MenuItem x:Name="BtnReporsClosedBugs" Header="Lukkede fejl" />  
  19. <MenuItem x:Name="BtnReporsNoAssignedBugs" Header="Ikke tildelte fejl" />  
  20. </ContextMenu>  
  21. <!--#endregion-->  
  22. <!--#region Administration-->  
  23. <ContextMenu x:Key="cmBtnAdministration" >  
  24. <MenuItem x:Name="BtnShowUsers" Header="Vis brugere" />  
  25. <MenuItem x:Name="BtnAddUser" Header="Tilføj bruger" />  
  26. <MenuItem x:Name="BtnEditAccount" Header="Ret konto" />  
  27. <MenuItem x:Name="BtnShowProjects" Header="Vis projekter" />  
  28. <MenuItem x:Name="BtnAddProject" Header="Tilføj projekt" />  
  29. <MenuItem x:Name="BtnManagement" Header="Vedligeholdelse" />  
  30. </ContextMenu>  
  31. <!--#endregion-->  
  32. <!--#region Preferences-->  
  33. <ContextMenu x:Key="cmBtnPreferences" >  
  34. <MenuItem x:Name="BtnMyInfo" Header="Min info" />  
  35. </ContextMenu>  
  36. <!--#endregion-->  
  37. <!--#region Preferences-->  
  38. <ContextMenu x:Key="cmBtnLists" >  
  39. <MenuItem x:Name="BtnMyBugs" Header="Mine fejl" />  
  40. <MenuItem x:Name="BtnNewBugs" Header="Nye fejl" />  
  41. <MenuItem x:Name="BtnAllBugs" Header="Alle fejl" />  
  42. <MenuItem x:Name="BtnUnAssignedBugs" Header="Ikke tildelte fejl" />  
  43. <MenuItem x:Name="BtnClosedBugs" Header="Lukkede fejl" />  
  44. </ContextMenu>  
  45. <!--#endregion-->  
  46. <!--#region Projects-->  
  47. <ContextMenu x:Key="cmBtnProjects" >  
  48. <MenuItem x:Name="BtnDashBoard" Header="Dashboard" />  
  49. <MenuItem x:Name="BtnNewBug" Header="Ny fejl" />  
  50. </ContextMenu>  
  51. <!--#endregion-->  
  52. <!--#endregion-->  
  53. </Window.Resources>  
  54. <Window.FocusVisualStyle>  
  55. <Style/>  
  56. </Window.FocusVisualStyle>  
  57. <StackPanel>  
  58. <Grid Margin="5">  
  59. <Grid.ColumnDefinitions>  
  60. <ColumnDefinition Width="250"/>  
  61. <ColumnDefinition Width="*"/>  
  62. <ColumnDefinition Width="150"/>  
  63. </Grid.ColumnDefinitions>  
  64. <Grid.RowDefinitions>  
  65. <RowDefinition Height="130" />  
  66. <RowDefinition Height="600" />  
  67. </Grid.RowDefinitions>  
  68. <Image Source="/Images/Logo/ANSI.gif" Grid.Row="0" Grid.Column="0" Height="100" />  
  69. <Image Source="/Images/Logo/logoBug.gif" Grid.Row="0" Grid.Column="2" Height="100" />  
  70. <Grid Grid.Column="1">  
  71. <Grid Margin="5">  
  72. <Grid.ColumnDefinitions>  
  73. <ColumnDefinition Width="*" />  
  74. <ColumnDefinition Width="*" />  
  75. <ColumnDefinition Width="*" />  
  76. <ColumnDefinition Width="*" />  
  77. <ColumnDefinition Width="*" />  
  78. <ColumnDefinition Width="*" />  
  79. </Grid.ColumnDefinitions>  
  80. <Grid.RowDefinitions>  
  81. <RowDefinition Height="180" />  
  82. </Grid.RowDefinitions>  
  83. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="0" Margin="5 0 0 0">  
  84. <Button x:Name="BtnProjects" Content="Projekter" Style="{StaticResource TopMenuButton}" Click="BtnTopMenu_Click" />  
  85. <Image Source="/Images/TopDivider.jpg" />  
  86. </StackPanel>  
  87. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="1" Margin="5 0 0 0">  
  88. <Button x:Name="BtnLists" Content="Lister" Style="{StaticResource TopMenuButton}" Click="BtnTopMenu_Click" />  
  89. <Image Source="/Images/TopDivider.jpg" />  
  90. </StackPanel>  
  91. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="2" Margin="5 0 0 0">  
  92. <Button x:Name="BtnPreferences" Content="Indstillinger" Style="{StaticResource TopMenuButton}" Click="BtnTopMenu_Click" />  
  93. <Image Source="/Images/TopDivider.jpg" />  
  94. </StackPanel>  
  95. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="3" Margin="5 0 0 0">  
  96. <Button x:Name="BtnAdministration" Content="Administration" Style="{StaticResource TopMenuButton}" Click="BtnTopMenu_Click" />  
  97. <Image Source="/Images/TopDivider.jpg" />  
  98. </StackPanel>  
  99. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="4" Margin="5 0 0 0">  
  100. <Button x:Name="BtnReports" Click="BtnTopMenu_Click" Content="Rapporter" Style="{StaticResource TopMenuButton}" />  
  101. <Image Source="/Images/TopDivider.jpg" />  
  102. </StackPanel>  
  103. <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="5" Margin="5 0 0 0">  
  104. <Button x:Name="BtnLogout" Click="BtnLogout_Click" HorizontalAlignment="Right" HorizontalContentAlignment="Right" Content="Log ud" Style="{StaticResource TopMenuButton}" />  
  105. <Image Source="/Images/TopDivider.jpg" />  
  106. </StackPanel>  
  107. </Grid>  
  108. </Grid>  
  109. <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Margin="25">  
  110. <views:CategoryView x:Name="CategoryView" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" />  
  111. </Grid>  
  112. </Grid>  
  113. </StackPanel>  
  114. </Window>  
And finally AnsiBug.MainWindow.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15. namespace AnsiBug  
  16. {  
  17. /// <summary>  
  18. /// Interaction logic for MainWindow.xaml  
  19. /// </summary>  
  20. public partial class MainWindow : Window  
  21. {  
  22. public MainWindow()  
  23. {  
  24. InitializeComponent();  
  25. HideUserControls();  
  26. //CategoryView.Visibility = Visibility.Visible;  
  27. }  
  28. private void HideUserControls()  
  29. {  
  30. //StudentView.Visibility = Visibility.Hidden;  
  31. //CategoryView.Visibility = Visibility.Hidden;  
  32. }  
  33. #region Topmenu knapper  
  34. private void BtnLogout_Click(object sender, RoutedEventArgs e)  
  35. {  
  36. this.Close();  
  37. }  
  38. private void BtnTopMenu_Click(object sender, RoutedEventArgs e)  
  39. {  
  40. var button = (Button)sender;  
  41. string btnName = "cm" + button.Name;  
  42. ContextMenu cm = this.FindResource(btnName) as ContextMenu;  
  43. cm.PlacementTarget = sender as Button;  
  44. cm.IsOpen = true;  
  45. }  
  46. #endregion  
  47. }  
  48. }  

Answers (2)