title: Null reference exception, combobox selected item
body: <p>I'm getting a <code>Null Reference Exception</code> at the line <code>ext = cmbFiletype.selecteditem.tostring();</code> but I can't work out why because it should only try this when <code>cmbfiletype.selecteditem</code> isn't null.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  namespace Drawings_Manager
  {
      /// &lt;summary&gt;
      /// Interaction logic for Find.xaml
      /// &lt;/summary&gt;
      public partial class Find : Window
      {
      string productpath;
      string path;
      string filepath;
      string bom;
      string ext;
  
      public Find()
      {
          InitializeComponent();
  
      }
  
      private void Find1_Initialized(object sender, EventArgs e)
      {
          string categorypath = "\\\\10.61.31.167\\Solidworks\\";
          foreach (string dir in System.IO.Directory.GetDirectories(categorypath))
          {
              int pathlength = dir.IndexOf("-");
              if (new DirectoryInfo(dir).Name.StartsWith("0"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("1"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("2"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("3"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("4"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("5"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("6"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("7"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("8"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("9"))
              {
                  cmbCategory.Items.Add(dir.Remove(0,categorypath.Length));
              }
          }
      }
  
      private void cmbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          cmbProduct.Items.Clear();
          cmbFileType.Items.Clear();
          lstFile.Items.Clear();
          lstPath.Items.Clear();
          productpath = "";
          path = "";
          filepath = "";
          ext = "";
          txtBOM.Document.Blocks.Clear();
          btnOpen.IsEnabled = false;
          productpath = "\\\\10.61.31.167\\Solidworks\\" + cmbCategory.SelectedItem;
          foreach (string dir in System.IO.Directory.GetDirectories(productpath))
          {
              int pathlength2 = dir.IndexOf("-");
              if (new DirectoryInfo(dir).Name.StartsWith("0"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("1"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("2"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("3"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("4"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("5"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("6"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("7"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("8"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
              else if (new DirectoryInfo(dir).Name.StartsWith("9"))
              {
                  cmbProduct.Items.Add(dir.Remove(0, productpath.Length + 1));
              }
          }
      }
  
      private void cmbProduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          cmbFileType.Items.Clear();
          lstFile.Items.Clear();
          lstPath.Items.Clear();
          path = "";
          filepath = "";
          ext = "";
          txtBOM.Document.Blocks.Clear();
          btnOpen.IsEnabled = false;
          cmbFileType.Items.Add("SLDASM");
          cmbFileType.Items.Add("SLDPRT");
          cmbFileType.Items.Add("SLDDRW");
          cmbFileType.Items.Add("PDF");
          cmbFileType.Items.Add("DXF");
          cmbFileType.Items.Add("JPG");
          cmbFileType.Items.Add("AVI");
          txtBOM.Document.Blocks.Clear();
          if (File.Exists("\\\\10.61.31.167\\Solidworks\\" + cmbCategory.SelectedItem + "\\"+ cmbProduct.SelectedItem + "\\" + "bom.txt"))
          {
              string bom = File.ReadAllText("\\\\10.61.31.167\\Solidworks\\" + cmbCategory.SelectedItem + "\\" + cmbProduct.SelectedItem + "\\" + "bom.txt");
              txtBOM.AppendText(bom);
          }
      }
  
      private void cmbFileType_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          lstFile.Items.Clear();
          lstPath.Items.Clear();
          path = "";
          filepath = "";
          ext = "";
          btnOpen.IsEnabled = false;
          if (cmbCategory.SelectedItem != "")
          {
              if (cmbFileType.SelectedItem != "")
              {
                  filepath = "\\\\10.61.31.167\\Solidworks\\" + cmbCategory.SelectedItem + "\\" + cmbProduct.SelectedItem;
                  ext = cmbFileType.SelectedItem.ToString();
                  foreach (string file in System.IO.Directory.GetFiles(filepath, "*." + cmbFileType.SelectedItem, SearchOption.AllDirectories))
                  {
                      int remove = file.LastIndexOf("\\");
                      lstPath.Items.Add(file);
                      lstFile.Items.Add(file.Remove(0, remove + 1));
                  }
              }
          }
      }
  
      private void btnOpen_Click(object sender, RoutedEventArgs e)
      {
          System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
          if (fileInfo.IsReadOnly == false)
              fileInfo.IsReadOnly = true;
          System.Diagnostics.Process.Start(@path);
      }
  
      private void lstFile_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          path = "";
          if (lstFile.SelectedItem != "")
          {
              int selection = lstFile.SelectedIndex;
              lstPath.SelectedIndex = selection;
              path = lstPath.SelectedItem.ToString();
              btnOpen.IsEnabled = true;
          }
      }
      }
  }
  </code></pre>
  
  <p>Do you have any ideas of why this is happening?</p>
  
  <p>Thanks in advance.</p>