-1

i have created a method where i join two sql tabels, with linq. when i debug and step into the method and i call it i can see that it is not null. But for som reason i am getting a nullreference when calling it.
Here is the method that i am calling.

public ItemIdentity GetIdentityInfo(int tradeItemId)
    {
        var query = (from item in _db.TradeItems
                     join identity in _db.ItemIdentities on item.itemIdentities equals identity.id
                     where item.id == tradeItemId
                     select identity);
        return query as ItemIdentity;
    }

And here is where i am calling it.

var tradeItemUnitDescriptor = GetIdentityInfo(tradeItemId).tradeItemUnitDescriptor;

is it not possible to call it like this?

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
Code_Viking
  • 618
  • 1
  • 8
  • 26

1 Answers1

3

You're missing a First() or FirstOrDefault(). This query returns an IQueryable<ItemIdentity>, which cannot be cast to a single ItemIdentity, so as ItemIdentity yields null.

So instead of return query as ItemIdentity, use return query.First().

You need to decide whether it is possible that the requested entity does not exist. By using First(), you let that call throw if the entity does not exist.

If you use FirstOrDefault(), this method can still return null, so you need to check for that in the call site:

var item = GetIdentityInfo(tradeItemId);
if (item != null)
{
    // do something with item
}

But apart from the missing cast, which is the core of your error, the rest is explained in What is a NullReferenceException, and how do I fix it?.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • so i should call .single() after the join method? – Code_Viking Oct 15 '15 at 11:37
  • 2
    Even here it is worth to mention that he can get null reference exception – Giorgi Nakeuri Oct 15 '15 at 11:37
  • .First() worked perfectly! thanks – Code_Viking Oct 15 '15 at 11:41
  • @KevinJensen Use the example @CodeCaster Provided, `.First()` can still throw a `NullReferenceException`. – Dayan Oct 15 '15 at 11:42
  • @Dayan nope. `First()` throws `InvalidOperationException` if the query yielded no results. – CodeCaster Oct 15 '15 at 11:43
  • @CodeCaster Learned something new but either way, OP is using First() which will still throw an Exception instead of returning `null`. – Dayan Oct 15 '15 at 11:51
  • @Dayan I explained that in my answer. If the record should exist, you must decide whether you want to return `null` or throw an exception. – CodeCaster Oct 15 '15 at 11:52
  • @CodeCaster Which brings me back to correcting the OP, use `FirstOrDefault` instead of `First` since the latter can possibly throw an exception and that is never better than returning null. – Dayan Oct 15 '15 at 11:54
  • @Dayan that is simply not true. It is an exception if it is an exceptional situation. OP needs to research the differences between returning `null` and throwing an exception, and choose the appropriate option. – CodeCaster Oct 15 '15 at 11:55
  • @CodeCaster Ok maybe I'm missing something here, but if you could avoid an exception why wouldn't you? Identifying the return as null can help you avoid just that and react accordingly. But I guess ultimately its the OPs choice. I was just curious as to why you feel that an exception in this case would be acceptable. – Dayan Oct 15 '15 at 11:59
  • @Dayan it depends on the layer this method is in and how the non-existence of a required record will propagate through the code into the UI. Instead of null checks at various levels, I'd rather throw a (custom!) exception than let the null propagate into the UI and show an error dialog there. For the custom exception, you'd still have to use `FirstOrDefault()` in this code though, followed by `if (null) throw EntityNotFoundException()`... See also [Should a retrieval method return 'null' or throw an exception when it can't produce the return value?](http://stackoverflow.com/questions/175532/). – CodeCaster Oct 15 '15 at 12:02
  • 1
    @CodeCaster That's what i was looking for :) "(custom!) exception than let the null propagate into the UI and show an error dialog there. ". Thanks! – Dayan Oct 15 '15 at 12:03