title: XMLNodeList null reference even after checking the next innertext value in advance
body: <p>I am trying to get the value from the next item, for example, if the next item is null, or doesnt exist, it will proceed to printing. The main problem i am having lies only at this part</p>
  
  <p>Main Question : </p>
  
  <pre><code>try
  {
   numCheck = productList[j+1]["Number"].InnerText;
  }
  catch (Exception ex)
  {
      numCheck = string.Empty;
  }
  
  if (numCheck != null)
  {
       try
       {                                
            mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);                                
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.ToString());
       }
  </code></pre>
  
  <p>I did a check before i tried if else just to make sure the value exist, however, even after it detects that it has a value, this line will throw an error NullReference</p>
  
  <pre><code>mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);
  </code></pre>
  
  <p>FULL Code just in case: </p>
  
  <pre><code>XmlDocument xml = new XmlDocument();
  xml.LoadXml(productItems);
  XmlNodeList productList = xml.SelectNodes("/Products/Product"); 
  
  for (int j = 0; j &lt; productList.Count; j++) 
  {
  
       itemCount++;
       int i = j + 1;
  
       string item = productList[j]["Number"].InnerText;         
  
       number = Data.getFullItemDetail(item);
  
              try
              {
                  #region mainItem
                  if (number == 0)
                  {
  
                      itemCount++;
                      itemName = productList[j]["Name"].InnerText;
  
                      try
                      {
                          numCheck = productList[i]["Number"].InnerText;
                      }
                      catch (Exception ex)
                      {
                          numCheck = string.Empty;
                      }
  
                      if (numCheck != null)
                      {
                          try
                          {                                
                              mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);                                
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.ToString());
                          }
  
                          if (mainNumber == 0)
                          {                                
                              foreach (string condimentItem in condiment)
                              {
                                  condimentText += condimentItem.ToString() + "\n";
                              }
  
                              string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
  
                              //print here, because the next item is another main item.
  
  
                              };
  
                              try
                              {
                                  p.Print();
                              }
                              catch (Exception ex)
                              {
                                  throw new Exception("Exception Occured While Printing", ex);
                              }
                          }
                          else
                          {
                              MessageBox.Show("Error");
                          }
  
                      }
                      else
                      {                            
                          foreach (string condimentItem in condiment)
                          {
                              condimentText += condimentItem.ToString() + "\n";
                          }
  
                          string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";                            
                      }
                  }
                  #endregion
  
                  #region CondimentItem
                  else if (number == 4)
                  {                        
                      condiment.Add(productList[j]["Name"].InnerText);
  
                      try
                      {
                          numCheck = productList[i]["Number"].InnerText;
                      }
                      catch (Exception ex)
                      {
                          numCheck = string.Empty;
                      }
  
                      //Check next item.                    
                      if (numCheck == null)
                      {                            
                          foreach (string condimentItem in condiment)
                          {
                              condimentText += condimentItem.ToString() + "\n";
                          }
  
                          string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
  
                          //print here, because the next item is another main item.
  
                      }
                      else
                      {
  
                          mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);
  
                          if (mainNumber == 0)
                          {
                              foreach (string condimentItem in condiment)
                              {                                    
                                  condimentText += condimentItem.ToString() + "\n";
                              }
  
                              string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
  
                              //print here, because the next item is another main item.
  
                          }
                      }
                  }
                  #endregion
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
  </code></pre>
  
  <p>Edited : Data.getFullItemDetail only returns 1 int value.</p>
  
  <pre><code>    public static int getFullItemDetail(string number)
      {
          int num = Convert.ToInt32(number);
          int numReturn;
  
          using (SqlConnection conn = getConnectionSAP())
          {
              SqlCommand comm = new SqlCommand("select belongcond from ------ where number = " + num, conn); 
  
              conn.Open();
              SqlDataReader dr = comm.ExecuteReader();
              while (dr.Read())
              {
                  num = Convert.ToInt32(dr["-------"]);
              }
              comm.Clone();
              comm.Dispose();
          }
  
          num = Convert.ToInt32(num.ToString().Substring(0, 1));
  
          return num;
      }
  </code></pre>