0

I'm trying to add a value to a List<>. The List<> is a column in my Users model. When I execute the Add() function I get the error message 'NullReferenceException was unhandled by user code'. I'm certain that the row that I'm attempting to modify exists. Here's a sample of my code:

var user2 = from check in myfirstDB.Users where check.Name.Equals("stu") select check;
user2.Single().discussionsIdList.Add(1);

Here's my Users model:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public int rank { get; set; }
    public List<int> discussionsIdList { get; set; }

}

Thanks in advance.

ekad
  • 14,436
  • 26
  • 44
  • 46
stzy
  • 11
  • 3
  • it's not the row that's causing the error..have you stepped through the code.. what is the value of `discussionIdList` when you step past the `var user2` line of code.. if it's null then you need to Initialize the List – MethodMan May 02 '15 at 21:50
  • Do you expect this `List` to be persisted in the database? If so, the question is: how? Fact is, there is no array column data type in SQL, and the property will be ignored by EF. – Gert Arnold May 02 '15 at 22:50
  • I've read where List<> of primitive types cannot be stored in the database but a List of model objects can So I've modified my model to store a List of the full object rather than the object Id. But the List is still not stored in the database and I still get the NullReferenceException error. – stzy May 03 '15 at 14:49
  • Then maybe it's time for a new question with the new code. But remember that a NRE is 1. Hard to diagnose from code snippets only. 2. Easy to spot by debugging. I'm amazed that you didn't seem to have checked whether `discussionsIdList` is null or not. That's the obvious culprit. A new question lacking this kind of check by yourself is bound to go down as duplicate of the NRE question again. – Gert Arnold May 03 '15 at 21:15

2 Answers2

4

The List is not being initialized and causing the Null Reference Exception. You could initialize it in the constructor.

public class User
{
    public User()
    {
        discussionsIdList = new List<int>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public int rank { get; set; }
    public List<int> discussionsIdList { get; set; }

}
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
  • Thanks for the response. I initialized the List in the constructor and the error message goes away. But ultimately I would like for the List to exist as a column in my Users table. Initializing the List in the object model does not allow the column to be stored. I also changed the List type to a full discussion object rather than discussion Id which is type int. I read where primitive List types can't be stored in an EF database. – stzy May 03 '15 at 15:14
0

This

user2.Single().discussionsIdList

is null, hence Add(...) cannot be invoked.

Add a check for null and this should sort your issue out (assuming null is a valid value for this property).

if(user2.Single().discussionsIdList != null)
    user2.Single().discussionsIdList.Add(1);
garryp
  • 5,508
  • 1
  • 29
  • 41