-3

I'm getting a Null Reference Exception at the line ext = cmbFiletype.selecteditem.tostring(); but I can't work out why because it should only try this when cmbfiletype.selecteditem isn't null.

Here's the 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
{
    /// <summary>
    /// Interaction logic for Find.xaml
    /// </summary>
    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;
        }
    }
    }
}

Do you have any ideas of why this is happening?

Thanks in advance.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
Tom
  • 13
  • 1
  • 6
  • Where is your combobox selection code? – Sajeetharan Jul 18 '14 at 06:51
  • 1
    `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Jul 18 '14 at 06:54
  • 1
    Also, please post only relevant code. 90% of the code posted is a noise. – Sriram Sakthivel Jul 18 '14 at 06:56
  • Thanks guys. I do understand the null reference exception but couldnt figure out how to fix it I didnt realize "" wasnt equal to null. I didnt know what would and wouldnt be relevant so I just posted the whole lot. thanks again for the help changing it to null worked. – Tom Jul 19 '14 at 08:30

1 Answers1

3

Change this

if (cmbFileType.SelectedItem != "")

to

if (cmbFileType.SelectedItem != null)
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • thanks mate worked perfect, stupid me didnt even think empty wasnt equal to null. – Tom Jul 19 '14 at 08:31