title: How do I fix this nullreferenceexception in my listbox?
body: <p>Sorry for asking this question again but, I don't really know how to debug this.<br>
  The debugger is giving me nullreferenceexception in this line:
          if (listBox1.SelectedItem.ToString() == "Chicken $15")
  I figure that the reason why it is giving me nullreferenceexception is because listbox1 is null so I think I have to initialize it.  But how?  I dont know how to initialize a listbox.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace lab6
  {
      public partial class Form1 : Form
  {
      double total = 0;
  
      int x = 0;
      string ord = "";
      public Form1()
      {
          InitializeComponent();
      }
  
      private void toolStripMenuItem1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
      }
  
      private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
      {
  
      }
  
      private void editToolStripMenuItem_Click(object sender, EventArgs e)
      {
  
      }
  
      private void placeToolStripMenuItem_Click(object sender, EventArgs e)
      {
          checkBox1.Checked = false;
          radioButton1.Checked = false;
          radioButton2.Checked = false;
          radioButton3.Checked = false;
          radioButton4.Checked = false;
  
          switch (checkBox1.Checked)
          {
           case true:
             total += 1;
              ord += "Water\n";
               break;
          }
  
          if (comboBox1.Text == "Extra Meat")
          {
              total += 1;
              ord += ord + "Extra Meat\n";
          }
          if (comboBox1.Text == "Extra Rice")
          {
              total += 1;
              ord += "Extra Rice\n";
  
          }
          if (comboBox1.Text == "Extra Veggies")
          {
              total += 1;
              ord += "Extra Veggies\n";
          }
  
  
  
  
  
          if (listBox1.SelectedItem.ToString() == "Chicken $15")
          {
              total += 15;
              ord += " Chicken\n";
  
          }
          else { }
  
  
  
              if (listBox1.SelectedItem.ToString() == "Pizza $8")  //&lt; my pathetic attempt to figure it out with intelisense
              {
                  total += 8;
                  ord += "Pizza\n";
              }
              else
              { 
  
              }
          if (listBox1.SelectedItem.ToString() == "Spaghetti $12")//&lt; my pathetic attempt to figure it out with intelisense
          {
              total += 12;
              ord += " Spaghetti\n";
          }
          else { }
          if (listBox1.SelectedItem.ToString() == "Fries $8")
          {
              total += 8;
              ord += " Fries\n";
          }
          else { }
          if (listBox1.SelectedItem.ToString() == "Burger $10")
          {
              total += 10;
              ord += " Burger\n";
          }
          else { }
  
          //radiobutton
          if (radioButton1.Checked)
          { 
              total+=5;
              ord += "Pineapple Juice\n";
          }
          if (radioButton2.Checked)
          {
              total+=6;
              ord += "Mango Juice\n";
          }
          if (radioButton3.Checked)
          {
              total+=7;
              ord += "Apple Juice\n";
          }
          if (radioButton4.Checked)
          {
              total+=8;
              ord += "Orange Juice\n";
          }
  
          MessageBox.Show("Order Done");
          listBox1.SelectedItems.Clear();
  
      }
  
      private void clearToolStripMenuItem_Click(object sender, EventArgs e)
      {
          ord = "";
          total = 0;
      }
  
      private void radioButton1_CheckedChanged(object sender, EventArgs e)
      {
  
      }
  
      private void label3_Click(object sender, EventArgs e)
      {
  
      }
  
      private void radioButton2_CheckedChanged(object sender, EventArgs e)
      {
  
      }
  
      private void radioButton4_CheckedChanged(object sender, EventArgs e)
      {
  
      }
  
      private void radioButton3_CheckedChanged(object sender, EventArgs e)
      {
  
      }
  
      private void checkBox1_CheckedChanged(object sender, EventArgs e)
      {
  
      }
  
      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
      private void label4_Click(object sender, EventArgs e)
      {
  
      }
  
      private void displayToolStripMenuItem_Click(object sender, EventArgs e)
      {
          MessageBox.Show("Order: " + ord+"\nTotal: "+total);
  
  
      }
  
      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
          this.Close();
      }
  }
  </code></pre>
  
  <p>}</p>