I have WPF project "LittleLibrary" and and it must enable adding and deleting books. So in Xaml i have form and one button
Here is how it looks like https://i.stack.imgur.com/fz4Pe.jpg
and in C# code i'm trying to serialization like this (I'm using Newtonsoft.Json)
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
public int Year { get; set; }
public string Cover { get; set; }
public int Availability { get; set; }
public string Description { get; set; }
}
public class RootObject
{
public List<Book> Book { get; set; }
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
RootObject rootObjectSerializacja = new RootObject();
Book newBook = new Book();
newBook.Title = titleTextBox.Text;
newBook.Author = authorTextBox.Text;
newBook.Genre = gatunekTextBox.Text;
newBook.Year = int.Parse(rokWydaniaTextBox.Text);
newBook.Cover = okladkaTextBox.Text;
newBook.Availability = int.Parse(dostepnoscTextBox.Text);
newBook.Description = opisTextBox.Text;
rootObjectSerializacja.Book.Add(newBook);
string content = JsonConvert.SerializeObject(rootObjectSerializacja);
File.WriteAllText("newJsonFile.json", content);
}
Code from my Xaml :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="200"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="300"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="7*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--Lewa strona -->
<Label x:Name="titleLabel" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center">Title:</Label>
<TextBox x:Name="titleTextBox" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150"></TextBox>
<Label x:Name="authorLabel" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center">Author:</Label>
<TextBox x:Name="authorTextBox" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150"></TextBox>
<Label x:Name="gatunekLabel" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center">Gentre:</Label>
<TextBox x:Name="gatunekTextBox" Grid.Column="1" Grid.Row="2" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="rokLabel" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Right" VerticalAlignment="Center">Year:</Label>
<TextBox x:Name="rokWydaniaTextBox" Grid.Column="1" Grid.Row="3" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="okladkaLabel" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="Center">Cover:</Label>
<TextBox x:Name="okladkaTextBox" Grid.Column="1" Grid.Row="4" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="dostepnoscLabel" Grid.Column="0" Grid.Row="5" HorizontalAlignment="Right" VerticalAlignment="Center">Avb:</Label>
<TextBox x:Name="dostepnoscTextBox" Grid.Column="1" Grid.Row="5" Width="150" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBox>
<Label x:Name="opisLabel" Grid.Column="0" Grid.Row="6" HorizontalAlignment="Right" VerticalAlignment="Center">Description:</Label>
<TextBox x:Name="opisTextBox" Grid.Column="1" Grid.Row="6" Width="150" ></TextBox>
</Grid>
<Button x:Name="btnAdd" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" Margin="10" Click="btnAdd_Click" >ADD</Button>
</Grid>
</Grid>
The problem is i have an Exception {"The object reference has not been set to the instance of the object."} System.NullReferenceException
And content is NULL. What i'm doing wrong ?