-1

am using a computed property inside a ShoppingCart model, but the product property is getting a null reference and throw a null reference error, can you please inform me if am computing the TotalWeight property properly.

[Key]
public int Id { get; set; }

public int ProductId { get; set; }
public virtual Products Product { get; set; }

[Display(Name = "Quantity")]
public int Quantity { get; set; }


[NotMapped]
public int TotalWeight
{
    get
    {
        return this.Quantity * this.Product.Weight;
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Because `Product` is `null`. Where are you setting it? –  Mar 28 '18 at 22:56
  • 4
    This is not related to ASP.NET MVC nor Core, please don't spam tags. If you are using Entity Framework Core this will only work if you included `Product` as part of the query, but you didn't post your query nor what framework you are using, so we can only guess. Please post a [mcve] – Camilo Terevinto Mar 28 '18 at 22:57
  • am passing product from a shoppingcart controller through a "public static void" function. – user2884954 Mar 28 '18 at 23:06
  • 1
    @user2884954 and? Use the debugger to check its value at that moment. Presumbly, it's null, or the weight property is null, or the quantity property in the shopping cart object is null. One of them is, you need to discover which one. The code above doesn't provide the answer definitively, but debugging will. Of course you need to be checking this at the time that the TotalWeight property is reference. Set a breakpoint in that get; method – ADyson Mar 28 '18 at 23:07
  • 2
    I'm personally not a fan of these approaches, if your data model or domain model don't represent the proper view data, then transform it into a proper representation. Don't hold a computed property that the ORM should ignore, just clutters and is harder to isolate. – Greg Mar 28 '18 at 23:22

2 Answers2

2

You should validate the computed property, since you do run the possibility of weight, product, and sub properties within product being null. Not sure if you encounter a failure what you would like to occur, but something along these lines should rectify.

public int TotalWeight 
{
     get
     {
         if(Weight == null || Product?.Weight == null)
              return 0;

         return Weight * Product.Weight;
     }
}

That would rectify your error, but you might have an issue with Entity Framework. You might not want to use the default lazy loading, but instead use eager loading.

Eager loading. When the entity is read, related data is retrieved along with it. This typically results in a single join query that retrieves all of the data that's needed. You specify eager loading in Entity Framework Core by using the Include and ThenInclude methods.

Some examples on it can be found here.

Greg
  • 11,302
  • 2
  • 48
  • 79
-1

I strongly believe either Quantity or Product or Weight ar simply null.

To find out which, use a breakpoint at the line

return this.Quantity * this.Product.Weight;

From there you will see which variable is null, and will be able to work your way up to see where you should initialize the object in question.

An unexpected nullref usually comes from another null object not being set in the first place

Gil Sand
  • 5,802
  • 5
  • 36
  • 78
  • This is not an answer, it is at best a bad comment. As I commented, the OP is most likely used to lazy loading of EF6 and doesn't expect the lack of lazy loading of EF Core – Camilo Terevinto Mar 28 '18 at 23:10
  • Quantity (and Weight most likely) is int, so it cannot be null. – M Bakardzhiev Mar 28 '18 at 23:15
  • 1
    @MBakardzhiev Yeah, but if Entity Framework hasn't loaded the associated object within the model the root Product would more than likely be null. Without more information can't verify though. – Greg Mar 28 '18 at 23:19
  • @Greg Yes, so this means the post opener needs to eagerly load Product (I am 99% sure he didn't). – M Bakardzhiev Mar 28 '18 at 23:21
  • @MBakardzhiev Yeah, exactly. That's why I mentioned that at the end of my answer. – Greg Mar 28 '18 at 23:23
  • var applicationDbContext = _context.ShoppingCarts.Include(s => s.ApplicationUser).Include(s => s.Product); – user2884954 Mar 28 '18 at 23:38
  • @user2884954 What is your relation type? 0/1 to 0/1? Maybe this shopping cart has no associated product. – M Bakardzhiev Mar 29 '18 at 00:04
  • this doesn't answer the question and provide a solution, it merely suggests how to debug – ADyson Mar 29 '18 at 08:27
  • Considering the amount of information available, I can't give a more precise answer. I don't know if these are POCO or primitives, I don't know what happened before, and I don't want to suggest simply adding `if != null` everywhere because it is just defensive programming and bad practice. To get to the root of the problem, I believe he's gotta find where the null is coming from. Hence my debugging suggestion. – Gil Sand Mar 29 '18 at 13:30
  • Yes but that's why it should be a comment and not an answer. It's a request for more information, which can then be used to provide a real answer – ADyson Mar 30 '18 at 05:23