1

Thsi Is My Model:

public VoyageReservedViewModel{
               public List<string> preFactorID { get; set; }
                public int Code{ get; set; } }

I want to Add String To List <String> by Thsi Code:

 VoyageReservedViewModel Obj = new VoyageReservedViewModel();
      foreach (var item in result)
      {    
          Obj.preFactorID.Add("My String Value");
      }

But I Reseved This Error:

An exception of type 'System.NullReferenceException' occurred in Sample.dll but was not handled in user code

S.Motamed
  • 183
  • 23
  • 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) – Sir Rufo Dec 09 '17 at 13:29
  • My Problem was Add String To List Its Not About [link]https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – S.Motamed Dec 09 '17 at 13:32
  • Your problem is the NullReferenceException and the answers in the linked question will guide you how to fix that. The cause is the same from all questions about NRE: you try to work with a reference which is null. period – Sir Rufo Dec 09 '17 at 13:39
  • BTW you also have some code and when it executes you get the NRE - it is a perfect duplicate – Sir Rufo Dec 09 '17 at 13:41
  • I can see my Error and msgError but i dont know why that msg shown to me, i had the problem that adding a string to list – S.Motamed Dec 10 '17 at 06:33

3 Answers3

2

You need to instantiate preFactorID which is a type of List<string>

   VoyageReservedViewModel Obj = new VoyageReservedViewModel();  
   Obj.preFactorID = new  List<string>(); 
   foreach (var item in result)
   {              
          Obj.preFactorID.Add("My String Value");
   }
Sajeetharan
  • 186,121
  • 54
  • 283
  • 331
1

You instantiated VoyageReservedViewModel but never instantiated its list property preFactorID. You can't add to the list if it has not been initialised.

D. Foley
  • 1,000
  • 8
  • 20
1

Obviously, preFactorID property is null! You need to initialize it, like this:

 VoyageReservedViewModel Obj = new VoyageReservedViewModel();
 Obj.preFactorID  = new List<string>();

Or using the object initializer like this:

VoyageReservedViewModel Obj = new VoyageReservedViewModel{
  preFactorID   = new List<string>()
};

Update I want to add something to this answer. If you just insert to this list in one specific place, if you add these IDs just in this foreach loop and you don't want to edit its values in anywhere else. I suggest that you use other type like IEnumerable<string> it gives your code more 'portability' using such a generic portable type. So your code become:

public VoyageReservedViewModel
{
   public IEnumerable<string> preFactorID { get; set; }
   public int Code{ get; set; } 
}
VoyageReservedViewModel Obj = new VoyageReservedViewModel();
var list = new List<string>();
foreach (var item in result)
{    
    list.Add("My String Value");
}
obj.preFactorID = list; // List<T> is an IEnumerable<T>
Mahmoud
  • 1,211
  • 1
  • 16
  • 30