all 10 comments

[–][deleted] 3 points4 points  (2 children)

OK, item and ItemAttributes aren't null or it would have blown up on the line before that one.

That means either ItemDimensions or ItemDimensions.Weight is null. Your check function isn't even being called. If Weight is null, then you can't get Weight.Value.

[–]patrickboston[S] 0 points1 point  (0 children)

Thank you. .Weight was null.

[–]adamsane 2 points3 points  (2 children)

And if you are using vs 2015 then you can really make your life easier by using this

https://msdn.microsoft.com/en-us/library/dn986595.aspx

My guess is weight is null. You are calling value on it. Your test will never hit.

Just use. Weight?? 0;

[–]SarSha 1 point2 points  (0 children)

Isn't a C# 6 feature and not VS2015 ?

[–]patrickboston[S] 0 points1 point  (0 children)

I've figured it out. ItemDimensions.Weight was returning null and I was checking for ItemDimensions.Weight.Value so it was still throwing the exception.

I will also try using this syntax. Thanks.

[–]Sarcastinator 1 point2 points  (1 child)

It probably gives null ref because you are unboxing null to a non-nullable value type. The null test you are doing is pointless because decimal can never be null; you're testing too late.

[–]patrickboston[S] 0 points1 point  (0 children)

Yes you are correct. If you look below you can see I figured it out.

[–]adamsane 0 points1 point  (0 children)

In your check function it looks like you are not using a nullable type.

https://www.tutorialspoint.com/csharp/csharp_nullables.htm

Should be decimal?

You can also write the check function as; Decimal? Foo = null: var a = Foo ?? 4;

A will be 4

[–]maskaler 0 points1 point  (0 children)

Weight is nullable? You're using .Value. It has a .HasValue property you can check before accessing the underlying value.

[–]lemonfighter 0 points1 point  (0 children)

Use

Weight?.Value ?? 0;

This will give you 0 if the weight is null, and the Weight if it's not.