locked
NullReferenceException when assigning a value to a class member RRS feed

  • Question

  • I got NullReferenceException: Object reference not set to an instance of an object in the following sample code, what is wrong? Please help.
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      public class NewItem
      {
        private string nameField;
        private string valueField;
    
        public string Name
        {
          get
          {
            return this.nameField;
          }
          set
          {
            this.nameField = value;
          }
        }
    
        public string Value
        {
          get
          {
            return this.valueField;
          }
          set
          {
            this.valueField = value;
          }
        }
      }
    
      class Program
      {
        static void Main(string[] args)
        {
          NewItem[] newItems = new NewItem[1];
          newItems[0].Name = "Name";         /// NullReferenceException @ this line
    
          Console.WriteLine(newItems[0].Name); 
        }
      }
    }
    
    
    Thanks.
    Thursday, July 14, 2011 5:35 PM

Answers

  • You didnt create a new class. You have only assign some value to an index of a class.

    So you have to create class (classes if more then one in array), and then you can assign values:

          NewItem[] newItems = new NewItem[1];
    
          //create "NewItems" clases in array:
          for (int i = 0; i < newItems.Length; i++)
          {
            newItems[i] = new NewItem();  //CREATE NEW CLASS - based on the index
            newItems[i].Name = "my name"; //assign new values
            newItems[i].Value = "my value"; //you can assign values out side of this loop too, just class HAS TO BE CREATED!!
          }
    
          Console.WriteLine(newItems[0].Name);
          Console.ReadLine();
    



    Mitja
    • Proposed as answer by ImmortalSmoke Monday, July 18, 2011 7:36 AM
    • Marked as answer by Jackie-Sun Monday, August 1, 2011 2:32 AM
    Thursday, July 14, 2011 5:45 PM

All replies

  • You didnt create a new class. You have only assign some value to an index of a class.

    So you have to create class (classes if more then one in array), and then you can assign values:

          NewItem[] newItems = new NewItem[1];
    
          //create "NewItems" clases in array:
          for (int i = 0; i < newItems.Length; i++)
          {
            newItems[i] = new NewItem();  //CREATE NEW CLASS - based on the index
            newItems[i].Name = "my name"; //assign new values
            newItems[i].Value = "my value"; //you can assign values out side of this loop too, just class HAS TO BE CREATED!!
          }
    
          Console.WriteLine(newItems[0].Name);
          Console.ReadLine();
    



    Mitja
    • Proposed as answer by ImmortalSmoke Monday, July 18, 2011 7:36 AM
    • Marked as answer by Jackie-Sun Monday, August 1, 2011 2:32 AM
    Thursday, July 14, 2011 5:45 PM
  • Check this example where we create 3 classes:

     

          NewItem[] newItems = new NewItem[3]; //3 classes!
    
          //create "NewItems" clases in array:
          for (int i = 0; i < newItems.Length; i++)
          {
            newItems[i] = new NewItem();
            newItems[i].Name = "my name " + (i + 1);
            newItems[i].Value = "my value " + (i + 1);
          }
    
          //lets override the class at index 3:
          newItems[2].Name = "overriden name is BUU";
          newItems[2].Value = "overriden value: NO value";
    
          //show all:
          foreach (NewItem item in newItems)
            Console.WriteLine("NAME: {0}, VALUE: {1}", item.Name, item.Value);
          Console.ReadLine();
    



    Mitja
    Thursday, July 14, 2011 5:48 PM
  • Hi,

     NewItem[] newItems = new NewItem[1];
          <strong> newItems[0] = new NewItem();
    </strong>      newItems[0].Name = "Name";     /// NullReferenceException @ this line
    
          Console.WriteLine(newItems[0].Name);
    

    The above code works. Pls try and let me know if there is anything else i can assist you with.

     


    Thank you and have great day! Vivek Kulkarni MCAD.net
    Friday, July 15, 2011 4:22 AM