In a sample MVC application that I am following from a book I have written this:
public ViewResult List(string category, int page = 1)
{
private IProductsRepository repository;
public int PageSize = 4;
public ProductController()
{
}
public ProductController(IProductsRepository productRepository)
{
this.repository = productRepository;
}
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ?
repository.Products.Count() :
repository.Products.Count(e => e.Category == category)
},
CurrentCategory = category
};
return View(viewModel);
}
and
public class ProductsListViewModel
{
public PagingInfo PagingInfo { get; set; }
public IEnumerable<Product> Products { get; set; }
public string CurrentCategory { get; set; }
}
When I want to run the application it crashes on the first method above saying Object reference not set to an instance of an object.
but we are using new
to create the object, so what is wrong?