getting system.nullreferenceexcepection

Aug 18 2019 4:56 AM
This is the test project
  1. public class UnitTest1  
  2. {  
  3.   
  4. //private ProductRepository<iproductrepository> repository;  
  5. //private ProductRepository<iproductdaterepository> productDateRepositor;  
  6. private IProductRepository productRepository;  
  7. private IProductDateRepository productDateRepository;  
  8. public ProductController productController;  
  9. List<productmodel> list;  
  10.   
  11. [TestInitialize]  
  12. public void setUpController()  
  13. {  
  14. //repository = new ProductRepository<iproductrepository>();  
  15. //productDateRepositor = new ProductRepository<iproductdaterepository>();  
  16. productController = new ProductController(productRepository, productDateRepository);  
  17. list = new List<productmodel>()  
  18. {  
  19. new ProductModel(){Id=1,tickersymbol = "AAP", Price = 11100 ,Date=DateTime.Parse("12/25/2018") },  
  20. new ProductModel(){Id=2,tickersymbol = "DAL", Price = 22200,Date=DateTime.Parse("11/27/2018")},  
  21. new ProductModel(){Id=3,tickersymbol = "GE", Price = 33300,Date=DateTime.Parse("12/26/2018")},  
  22. new ProductModel(){Id=4,tickersymbol = "AMZN", Price = 44400,Date=DateTime.Parse("11/27/2018")},  
  23. new ProductModel(){Id=5,tickersymbol = "FNB", Price = 55500,Date=DateTime.Parse("12/28/2018")},  
  24. };  
  25. }  
  26. [TestMethod]  
  27. public void Index()  
  28. {  
  29. //Arrange  
  30. var controller = productController;  
  31. //Act  
  32. var result = controller.Index("AAP"as ViewResult;  
  33. var tickerdetails = (ProductModel)result.ViewData.Model;  
  34. //Assert  
  35. Assert.AreEqual("AAP",tickerdetails.tickersymbol );  
  36. }  
While running this I am getting Message: Test method UnitTestProject2.UnitTest1.Index threw exception:
 
System.NullReferenceException: Object reference not set to an instance of an object.
 
This is the Repository
  1. public class ProductRepository : IProductRepository  
  2. {  
  3. private List<ProductModel> _products;  
  4. public ProductRepository()  
  5. {  
  6. _products = new List<ProductModel>()  
  7. {  
  8. // Add products for the Demonstration  
  9. new ProductModel(){Id=1,tickersymbol = "AAP", Price = 11100 ,Date=DateTime.Parse("12/25/2018") },  
  10. new ProductModel(){Id=2,tickersymbol = "DAL", Price = 22200,Date=DateTime.Parse("11/27/2018")},  
  11. new ProductModel(){Id=3,tickersymbol = "GE", Price = 33300,Date=DateTime.Parse("12/26/2018")},  
  12. new ProductModel(){Id=4,tickersymbol = "AMZN", Price = 44400,Date=DateTime.Parse("11/27/2018")},  
  13. new ProductModel(){Id=5,tickersymbol = "FNB", Price = 55500,Date=DateTime.Parse("12/28/2018")},  
  14. };  
  15. }  
  16. public IEnumerable<ProductModel> GetPrice(string Name, DateTime? Date)  
  17. {  
  18. if (Date != null && Name!="")  
  19. {  
  20. var product = _products.Where(x => x.tickersymbol.StartsWith(Name) && x.Date == Date).ToList();  
  21. return product;  
  22. }  
  23. else  
  24. {  
  25. return _products.ToList();  
  26. }  
  27. }  
  28. ProductModel IProductRepository.Get(int id)  
  29. {  
  30. return _products.Find(p => p.Id == id);  
  31. }  
  32. IEnumerable<ProductModel> IProductRepository.GetAll(string Name)  
  33. {  
  34. if (Name != "")  
  35. {  
  36. var product = _products.Where(x => x.tickersymbol.StartsWith(Name) || Name == null).ToList();  
  37. return product;  
  38. }  
  39. else  
  40. {  
  41. return _products.ToList();  
  42. }  
  43. }  
  44. IEnumerable<ProductModel> IProductRepository.GetAll()  
  45. {  
  46. return _products.ToList();  
  47. }  
  48. }  
  49. }  

Answers (3)