title: Unhandled Exception: NullReferenceException:Object Reference not set to the
body: <p>I know i am making a silly mistake but unfortunately i am unable to find it even after a lot of Debug.
  I have made a class <strong>"naivebayes"</strong> and other class <strong>Connect</strong></p>
  
  <p>======================== This is a method of <strong>Connect</strong>==================</p>
  
  <pre><code>  public NaiveBayes[] ReadOBj()
      {
          SqlConnection conn = new SqlConnection(connectionString);
          conn.Open();
  
          SqlCommand command = new SqlCommand(@"SELECT NAME, CODE, DEPARTMENT, TS, CD, REPEAT, CORE, [Content], Grade  FROM            Transcript WHERE        (Grade &lt;&gt; 'UNKNOWN')", conn);
          SqlDataReader reader = null;
          reader=command.ExecuteReader();
          NaiveBayes[] a=new NaiveBayes[10];
          NaiveBayes1.NaiveBayes na = new NaiveBayes();
  
          int i = 0;
          while (reader.Read())
          {
              i++;
  
                  string Namee = (string)reader["NAME"];
                  Console.WriteLine(Namee);
                  na.Name = Namee;
  
              string depte=reader["DEPARTMENT"].ToString();
              na.Dept = depte;
  
               string core=   reader["CORE"].ToString();
              Boolean.TryParse(core,out na.Core);
  
              string rpet=reader["REPEAT"].ToString();
              Boolean.TryParse(core,out na.Repeat);
              int tse,cde;
  
                  int.TryParse(reader["TS"].ToString(),out tse) ;
                  int.TryParse(reader["CD"].ToString(),out cde);
              na.TS=tse;
              na.CD=cde;
                  string contente=reader[7].ToString();
                  na.Content = contente;
  
  
              string grade=reader["Grade"].ToString();
              na.Grade = grade;
  
              a[i] = na;
  
  
          }
  
          conn.Close();
          return a;
      }
  </code></pre>
  
  <p>1) The problem is when i try to access attributes of <strong>NaiveBayes</strong> it gives <strong>null reference exception</strong>. </p>
  
  <pre><code>     Forexample :
            a[i].Name="ABC";
           This will raise the following Exception.
      Unhandled Exception: System .NullReferenceException :Object Reference is not set to an instance of object
  </code></pre>
  
  <p>2)The 2nd problem is that all object in a[i] must have distinct values but the value is replicated(of last iteration) </p>
  
  <pre><code>    Forexample when i=2 ,and a[1].Name was "IstName" .and a[2].Name must be "2ndName". At the end both a[1].Name and a[2].Name has same value"2ndName" 
  </code></pre>
  
  <p>================================This is <strong>NaiveBayes</strong> class======================</p>
  
  <pre><code> namespace NaiveBayes1
   {
    public class NaiveBayes 
    {
      public string Name ;
      public string Dept ;
      public string Content ;
      public string Grade ;
      public Boolean Core ;
      public Boolean Repeat;
      public int TS ;
      public int CD ;
  
  
      public NaiveBayes()
      {
  
      Name = "";
      Dept = "";
      Content = "";
      Grade = "";
      Core = false;
      Repeat = false;
      TS = 0;
      CD = 0;    
      }
  }
  </code></pre>
  
  <p>================Answer of Problem 2========================</p>
  
  <pre><code>   NaiveBayes[] na = new NaiveBayes[5];
     NaiveBayes[0].Name ="ABC" // NaiveBayes[0] is null.  The array was allocated but not initialized.
                 // There is no NaiveBayes class to set the Name for.
  </code></pre>
  
  <p>Full Answer is here <a href="https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it">What is a NullReferenceException, and how do I fix it?</a></p>