ProductRepository Throwing Null Reference Exception

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello!
  am getting am error "Null Reference Exception" while trying to query product repository.
Code below.. don't know that am doing wrong.

private readonly IRepository<Product> _productVendorRepository;

public VendorService(IRepository<Product> productVendorRepository)
        {
            this._productVendorRepository = productVendorRepository;
            
            this._vendorService = EngineContext.Current.Resolve<IVendorService>();
            this._cacheManager = EngineContext.Current.Resolve<ICacheManager>();
       }

public virtual IList<VendorOrder> getVendorSoppingCartItems(IList<ShoppingCartItem> shoppingCartItems)
        {
            var vendorOrders = new List<VendorOrder>();
          

            foreach (var item in shoppingCartItems)
            {
          

                var query = from ps in _productVendorRepository.Table
                          where ps.Id == item.Product.Id
                            select ps.Vendor;
                var vendor = query.FirstOrDefault();


                int index = vendorOrders.FindIndex(s => s.vendor == vendor);
                if (index >= 0)
                {
                    vendorOrders[index].ShoppingCartItems.Add(item);
                }
                else
                {
                    var newVendorOrder = new VendorOrder();
                    newVendorOrder.vendor = vendor;
                    newVendorOrder.ShoppingCartItems.Add(item);
                    vendorOrders.Add(newVendorOrder);
                }
            }
            return vendorOrders;
        }




This Line Trowing the Error!


                var query = from ps in _productVendorRepository.Table
                          where ps.Id == item.Product.Id
                            select ps.Vendor;
                var vendor = query.FirstOrDefault();

i will Appreciate your help!
Thanks.
6 years ago
height wrote:



                var query = from ps in _productVendorRepository.Table
                          where ps.Id == item.Product.Id
                            select ps.Vendor;
                var vendor = query.FirstOrDefault();




When object is null then you will not able to access there property
item.product.Id


You can check condition like:

if(item.product != null) {
var query = from ps in _productVendorRepository.Table
                          where ps.Id == item.Product.Id
                            select ps.Vendor;
}


Or

if query is returning null result then you will not able to use FirstOrDefault() function.

You can check condition like:

if(query != null) {
var vendor = query.FirstOrDefault();
}


Try this and let me if any problems

Thanks,
Jatin
6 years ago
height wrote:
This Line Trowing the Error!


Which line?  An IQueryable will never be null with LINQ.  (It might be empty "collection".)
Maybe your _productVendorRepository is null.  Set a breakpoint and test it.
6 years ago
New York wrote:
This Line Trowing the Error!

Which line?  An IQueryable will never be null with LINQ.  (It might be empty "collection".)
Maybe your _productVendorRepository is null.  Set a breakpoint and test it.


_productVendorRepository should not null because they properly created object in there constructor
6 years ago
Hello!
   Thanks everyone for your help and contribution, have actully get it to work when took the method off vendor service and put it inside product service... I don't know why it didn't work while in vendorservice... but it's working fine now.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.