4

I have generated the data model in Entity Framework using an existing database with the tables filled out.

I am trying to access the data from a table and populate a datagrid in WPF but keep getting a null reference exception.

The exception is generated here:

  pubilc List<item> GetAllItems() 
  {
         using (var context = new DbEntities())
         {
             if (context.items != null)
                 return context.items.ToList()  //exception generated here
             else 
                 return new List<item>();
         } 
   }
kr13
  • 527
  • 4
  • 8
  • 22
  • 2
    Which line in your code is getting the exception? Have you checked that `context.items` is not null? – Bob. Aug 21 '13 at 17:53
  • I put a check if (context.items != null). It returns true the code runs and I get "Object reference not set to an instance of an object" – kr13 Aug 21 '13 at 18:00
  • Assuming you are using VS, can you set a watch on context.items to confirm it is populated? – IllusiveBrian Aug 21 '13 at 18:17
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 21 '13 at 18:54
  • What's the stack trace? Is `items` just a DbSet? What's the database provider? – Gert Arnold Aug 21 '13 at 22:38
  • I hope i am not late but hey, future askers may want to check this possible solution. I got in the same situation and while debugging, I found out context. value turned to null, all you need to do is check the .Context.cs There is a high chance that table of yours became internal instead of public. Just change it back to public if that is the case and it should be good. – Rozen Aug 22 '14 at 20:51
  • 1
    A shame this was closed as a duplicate - the proposed answer has nothing to do with the OP's question. Anyway - the reason you are running into this issue likely to do with how you've modeled the primary keys in your EF entity. If the property(ies) that is modeled as the PK has a NULL value in the database, then you'll get this error when EF tries to do a ToList() on the DbSet. I've seen this occur, especially when mapping to a view, and you are manually decorating your properties with a [Key] attribute, and the properties you assumed would never be null actually were. – RMD Oct 20 '15 at 21:15

1 Answers1

0

If performance is not an issue, you can call Count() method and check if there are any items to return.

public List<item> GetAllItems() 
{
    using (var context = new DbEntities())
    {
        if (context.items.Count() > 0)
            return context.items.ToList()  //exception generated here
        else 
            return new List<item>();
    }
}
Gustavo Azevedo
  • 113
  • 1
  • 7
  • 3
    Not necessary. An empty set will return an empty list. Also, don't use Count()>0, use Any(). – CodeCaster Aug 21 '13 at 18:12
  • I tried the code. Now the exception is generated at the if condition using both Count() and Any() – kr13 Aug 21 '13 at 18:18
  • So, the list passes the null test, but can't execute any method, saying the list is null? – Gustavo Azevedo Aug 21 '13 at 18:25
  • @Gustavo That is right. Now the exception is generated at the line "if (context.items.Count() > 0)" – kr13 Aug 21 '13 at 18:30
  • @Gustavoa.k.a.Guto `ToList()` is throwing a `NullReferenceException` because `items` is null. How would you call `Count()` on a null reference? – user2674389 Aug 21 '13 at 19:02
  • @user2674389 But it passes the null check – kr13 Aug 21 '13 at 19:04
  • @kr13 Really? That is odd. You should post the entire callstack. – user2674389 Aug 21 '13 at 19:09
  • what is the type of the items object? – Claies Aug 21 '13 at 19:23
  • @AndrewCounts DbSet – kr13 Aug 21 '13 at 19:28
  • @kr13 I think I am having the same issue. I call Count or any method that would evaluate the IQueryable and it throws a NullReference. The stack trace seems to show that the null exception is coming from underneath the hood. It's almost like the DB can't get accessed. The IQueryable is not null but when trying to evaluate it, something inside the evaluation throws the Null Exception. – Ian Kirkpatrick Jun 05 '19 at 15:44