-1

I'm writing a program that uses Entity Framework and linq. The problem is with the query arInvArea and especially in the where clause. In WithSwimmingPool there are zero values and therefore I get null reference exception. How can I catch such exception in the where clause. Other solutions in Stackoverflow didn't help me. Thanks

private ObjectContactsRow CreateNewRow(AreaInventory arInv)
{
    // Here in the where clause I get exception ! WithSwimmingPool is from type bool
    var arInvArea = arInv.Area.Where(p => p.WithSwimmingPool)
                              .Select(p => p.Units(ReportDate))
                              .FirstOrDefault();

    return new ObjectContactsRow()
               {
                   areaSize =  arInvArea  
               };
}

public partial class Area 
{
    public bool WithSwimmingPool => AreaArt.AreaUnit_ID == "SWMP";
}

public class ObjectContactsRow 
{
    public double areaSize { get; set; }

    public override object[] GetExcelRow()
    {        
        var index = 0;

        Row[index++] = areaSize;

        return Row;
    }
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Yves
  • 117
  • 1
  • 7
  • I think If `WithSwimmingPool` is bool you should use `Any` instead of `where`. – hassan.ef May 05 '19 at 14:01
  • 1
    What is exactly `null` in your example, is it `arInv.Area?, if not, are there any `null` in that collection? In addition, would be good if you post the `AreaInventory` class. – Ofiris May 05 '19 at 14:11
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan May 05 '19 at 14:12
  • 1
    `AreaArt` == null. – Stefan May 05 '19 at 14:13
  • Hello Ofiris i mean with null empty. The Database record is null. – Yves May 05 '19 at 14:16
  • `In WithSwimmingPool there are zero values and therefore I get null reference exception.` - you just said `WithSwimmingPool` was a bool property, how can there be several values "in it"? If you meant that no records in `arInv.Area` satisfy the `p => p.WithSwimmingPool` condition, then no, that does not produce a null reference exception. – GSerg May 05 '19 at 15:59

1 Answers1

1

You have to change your query to:

var arInvArea = arInv.Area.Where(p => p != null && p.WithSwimmingPool)
                          .Select(p => p.Units(ReportDate))
                          .FirstOrDefault();

Your collection may include NULL indexes and that leads to p == null in your query.

If by any chance you are using C# 8, you can enable nullable by including #nullable enable in your code. That then points out to all code segments with a null reference error potency.

Transcendent
  • 4,866
  • 4
  • 19
  • 46
  • `Your collection may include NULL indexes` - how do you know? – GSerg May 05 '19 at 16:00
  • @GSerg: I said `May` include null indexes, that is a probability. – Transcendent May 05 '19 at 16:05
  • Yes, it is a probability, and so are many other potential reasons, but you are giving this one as the only answer. – GSerg May 05 '19 at 16:06
  • @GSerg: well, in his select method, `Units` is a function so it cannot be null itself but can be associated with a null reference. On the other hand, `boolean` is value type which means it is not nullable. The only nullable here is `p`. So for that reason, my post is the only answer. – Transcendent May 05 '19 at 16:08
  • Could `arInv` or `arInv.Area` not be null? – GSerg May 05 '19 at 16:11
  • @GSerg: That is right, it can be null and but that would be too simple in my opinion. – Transcendent May 05 '19 at 16:12
  • Thanks fot the answers. The debugger show me: p.WithSwimmingPool =error CS0103: The name "p" does not exist in the current context. I have no idea. – Yves May 05 '19 at 18:48
  • @Yves Putting `p.WithSwimmingPool` in an immediate window makes no sense because `p` only exists in the lambda to which you have no access. If you collection indeed has null elements, include the filter on `p != null` like this answer suggests. You don't need to break into the lambda to verify that. – GSerg May 05 '19 at 19:03