2

I am creating a new theme in nopcommerce,
I want to print all categories in view,

I have wrote follwing code to retrieve all subcategories within a cartegory,
This call(on line 2) fails with Object reference not set to instance of onject error.

var _engine = new NopEngine();
var categoryService = _engine.Resolve<ICategoryService>();
var L1Categories = from p in categoryService.GetAllCategoriesByParentCategoryId(24)
                    where (p.Deleted==false)
                    select p;
Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98
  • http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Soner Gönül Sep 05 '15 at 11:00
  • @SonerGönül My code is valid and it is used in same way in other modules of nopcommerce – Nitin Sawant Sep 05 '15 at 11:05
  • In which location of nopcommerce you have written this portion of the code. Nop engine initialisation only happen at global app start event. Usually, no need to set it again. – Jeyara Sep 06 '15 at 22:57

1 Answers1

4

You should use the following code to resolve ICategoryService:

var categoryService = EngineContext.Current.Resolve<ICategoryService>();

Do not forget to add appropriate "using" directives

Andrei M
  • 3,429
  • 4
  • 28
  • 35