-1

I have a viewmodel and it contains two viewmodel in ReaderBorrowViewModel, I'm not sure it's best practice but it makes sense to me. In GetReaderDetailModel method i got borrows from db. After every borrowed book i tried to get data for ReaderDetailViewModel which contains BookStateModel and BookViewModel.Getting all borrowed books i added to list in ReaderBorrowModel to send it client.I got Object reference not set to an instance of an object while i creating an instance of ReaderBorrowModel. What's my fault ?

public class BookStateViewModel
{
    public int BookStateKey { get; set; }
    public string Name { get; set; }
}

public class BookViewModel
{
    public int BookKey { get; set; }
    public string ISBN { get; set; }
    public string  Name { get; set; }
    public string Author { get; set; }
}

public class ReaderBorrowModel
{
    public int BorrowKey { get; set; }        
    public BookViewModel Book { get; set; }
    public BookStateViewModel BookState { get; set; } 
}

public class ReaderDetailViewModel
{
    public MemberViewModel Member { get; set; }
    public List<ReaderBorrowModel> Borrows { get; set; }  
}


    public async Task<ReaderDetailViewModel> GetReaderDetailModel(int memberKey)
    {
        ReaderDetailViewModel result = new ReaderDetailViewModel();
        var borrows= borrowService.SelectIncludeMany(x => x.Member,k=>k.Book, d=>d.BookState).Where(x=>x.MemberKey== memberKey);

        var member= borrows.Select(x => x.Member);

        try
        {
            borrows.ToList().ForEach(o =>
            {
                var borrow = new ReaderBorrowModel
                {
                    BorrowKey = o.BorrowKey,
                    Book = new BookViewModel
                    {
                        BookKey = o.Book.BookKey ,
                        Name = o.Book.Name ,
                        ISBN = o.Book.ISBN ,
                        Author = o.Book.Author.Name
                    },
                    BookState = new BookStateViewModel
                    {
                        BookState Key = o.BookState Key,
                        Name= o.BookState.Name
                    }
                };
                result.Borrows.Add(odunc);
            });

        }
        catch (Exception ex)
        {

            throw ex;
        }

        return result;
    }
Trinity
  • 486
  • 8
  • 27
  • Maybe you've forgot to include `Book.Author` ? – ASpirin Mar 29 '20 at 12:37
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ASpirin Mar 29 '20 at 12:37

1 Answers1

0

You have not initialized the property to a new list. Try the below snippet. Once the list is set to a new list, you don't get null reference exception

public class ReaderDetailViewModel
{
    public ReaderDetailViewModel() {
        Borrows = new List<ReaderBorrowModel>();
    }
    public MemberViewModel Member { get; set; }
    public List<ReaderBorrowModel> Borrows { get; set; }  
}

Note As a good practice, you can always initialize the list / dictionary etc to a new list or new dictionary etc so that you don't run into null reference exceptions.

Saravanan
  • 7,637
  • 5
  • 41
  • 72