title: C# null reference error when using a global string variable
body: <p>I'm making a program that manages users data, for example when first user likes Lamborghini Aventador and second user likes Ferrari F12b, the program asks user what they like. According to the program, first user enters Lamborghini Aventador and the second one enters F12b. To make the program, I used a C# Windows form project in Microsoft Visual Studio 2013.</p>
  
  <p>Then I made a button1 and a textbox1, when the first user types in the textbox1 what he likes (which is Lamborghini Aventador) he must click on the button1. After clicking, the software is ready for the next user to type his favorite car which is Ferrari F12b.</p>
  
  <p>Then I made a button2 and textbox2, when you type "1" on the textbox2 and click the button2, it shows you what car the first user likes (Aventador) int the textbox2.</p>
  
  <p>Actually I created a class called "car" which has a string which is called "fav_car" AND a integer called "user_id". when user types his favorite car he actually types in the string of the class.</p>
  
  <p>then I wrote a code like this:</p>
  
  <p>global variables:</p>
  
  <pre><code>car []a=new car [2]; 
  int i=0;
  </code></pre>
  
  <p>for the button1:</p>
  
  <pre><code>a[i].fav_car=textbox1.Text;
  a[i].user_id=i;
  i++;
  </code></pre>
  
  <p>for the button2:</p>
  
  <pre><code>for (int j=0; j&lt;=2; j++) {
      if (textbox2.text==a[i].user_id) {
          textbox2.text==a[i].fav_car;
      }
  }
  </code></pre>
  
  <p>But when compiling the code, it says </p>
  
  <blockquote>
    <p>Null Reference error </p>
  </blockquote>
  
  <p>in the <code>if (textbox2.text==a[i].user_id)</code> line.<br>
  How to solve it and why that happened? My <code>a[i]</code> string was global, it shouldn't have happened!</p>
  
  <p>**EDIT: <em>a[i] is array (not string), I made mistake in writing and it was "For (int j=0; j&lt;2 ;j++) and also for button2 it was a[j] not a[i] those were mistakes in my post,</em> My real problem was the null reference of the "textbox2.text==a[i].user_id" line. **</p>