TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gitanjali Padhihari
NA
32
1.1k
getting system.nullreferenceexcepection
Aug 18 2019 4:56 AM
This is the test project
public
class
UnitTest1
{
//private ProductRepository<iproductrepository> repository;
//private ProductRepository<iproductdaterepository> productDateRepositor;
private
IProductRepository productRepository;
private
IProductDateRepository productDateRepository;
public
ProductController productController;
List<productmodel> list;
[TestInitialize]
public
void
setUpController()
{
//repository = new ProductRepository<iproductrepository>();
//productDateRepositor = new ProductRepository<iproductdaterepository>();
productController =
new
ProductController(productRepository, productDateRepository);
list =
new
List<productmodel>()
{
new
ProductModel(){Id=1,tickersymbol =
"AAP"
, Price = 11100 ,Date=DateTime.Parse(
"12/25/2018"
) },
new
ProductModel(){Id=2,tickersymbol =
"DAL"
, Price = 22200,Date=DateTime.Parse(
"11/27/2018"
)},
new
ProductModel(){Id=3,tickersymbol =
"GE"
, Price = 33300,Date=DateTime.Parse(
"12/26/2018"
)},
new
ProductModel(){Id=4,tickersymbol =
"AMZN"
, Price = 44400,Date=DateTime.Parse(
"11/27/2018"
)},
new
ProductModel(){Id=5,tickersymbol =
"FNB"
, Price = 55500,Date=DateTime.Parse(
"12/28/2018"
)},
};
}
[TestMethod]
public
void
Index()
{
//Arrange
var controller = productController;
//Act
var result = controller.Index(
"AAP"
)
as
ViewResult;
var tickerdetails = (ProductModel)result.ViewData.Model;
//Assert
Assert.AreEqual(
"AAP"
,tickerdetails.tickersymbol );
}
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
public
class
ProductRepository : IProductRepository
{
private
List<ProductModel> _products;
public
ProductRepository()
{
_products =
new
List<ProductModel>()
{
// Add products for the Demonstration
new
ProductModel(){Id=1,tickersymbol =
"AAP"
, Price = 11100 ,Date=DateTime.Parse(
"12/25/2018"
) },
new
ProductModel(){Id=2,tickersymbol =
"DAL"
, Price = 22200,Date=DateTime.Parse(
"11/27/2018"
)},
new
ProductModel(){Id=3,tickersymbol =
"GE"
, Price = 33300,Date=DateTime.Parse(
"12/26/2018"
)},
new
ProductModel(){Id=4,tickersymbol =
"AMZN"
, Price = 44400,Date=DateTime.Parse(
"11/27/2018"
)},
new
ProductModel(){Id=5,tickersymbol =
"FNB"
, Price = 55500,Date=DateTime.Parse(
"12/28/2018"
)},
};
}
public
IEnumerable<ProductModel> GetPrice(
string
Name, DateTime? Date)
{
if
(Date !=
null
&& Name!=
""
)
{
var product = _products.Where(x => x.tickersymbol.StartsWith(Name) && x.Date == Date).ToList();
return
product;
}
else
{
return
_products.ToList();
}
}
ProductModel IProductRepository.Get(
int
id)
{
return
_products.Find(p => p.Id == id);
}
IEnumerable<ProductModel> IProductRepository.GetAll(
string
Name)
{
if
(Name !=
""
)
{
var product = _products.Where(x => x.tickersymbol.StartsWith(Name) || Name ==
null
).ToList();
return
product;
}
else
{
return
_products.ToList();
}
}
IEnumerable<ProductModel> IProductRepository.GetAll()
{
return
_products.ToList();
}
}
}
Reply
Answers (
3
)
Help me to develop the project.
how to redirect to another page from gridview in asp net wit