0

Trying to figure out why this isn't working, the list is to retrieve photos using the combobox item (which lists local HDDs root address as items) when selected that item it is converted into a string and supposed to be used as a path for the GetFiles Method but screws up on the (string path = ) line when running, I get "object reference not set to instance of an object" much appreciated If someone could tell me what is going wrong

public List<Photos> LoadImages ///List Retrieves and Loads Photos
    {

        get
        {
            List<Photos> Image = new List<Photos>();
            string path = HDDSelectionBox.SelectedItem.ToString(); //ComboBox SelectedItem Converted To String As Path
            foreach (string filename in Directory.GetFiles(path, "*jpg")) 
            {
                try
                {
                    Image.Add( //Add To List
                        new Photos(
                            new BitmapImage(
                                new Uri(filename)),
                                System.IO.Path.GetFileNameWithoutExtension(filename)));
                }
                catch { } //Skips Any Image That Isn't Image/Cant Be Loaded
            }
            return Image;
        }
    }
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • This most likely means that either `HDDSelectionBox` or `HDDSelectionBox.SelectedItem` is null. Have you checked for that? – ChrisK Nov 23 '13 at 23:12
  • mm I read into that but because I'm learning as I'm going this is new to me, my aim is to load images only when the combobox isnt null i.e only after user has selected a item from the combobox, i've a feeling its making a list right as the program loads which then of course would be null to begin with – Nathan Anderson Nov 23 '13 at 23:15

1 Answers1

0

You should put . before the file extension in the line:

Directory.GetFiles(path, "*.jpg")

You also need to check if HDDSelectionBox.SelectedItem is not null:

public List<Photos> LoadImages ///List Retrieves and Loads Photos
{
    get
    {
        List<Photos> images = new List<Photos>();
        if (HDDSelectionBox.SelectedItem != null)
        {
            string path = HDDSelectionBox.SelectedItem.ToString(); //ComboBox SelectedItem Converted To String As Path
            foreach (string filename in Directory.GetFiles(path, "*.jpg"))
            {
                try
                {
                    images.Add( //Add To List
                        new Photos(
                            new BitmapImage(
                                new Uri(filename)),
                                System.IO.Path.GetFileNameWithoutExtension(filename)));
                }
                catch { } //Skips Any Image That Isn't Image/Cant Be Loaded
            }
        }
        return images;
    }
}

Also, this is probably better suited to a method rather than a property as it does quite a lot of processing...

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • awesome I just wandering also where my new Lists line would go? apreciate the help so far, I had thought a while back of checking for null but it is currently expecting a get or set accessor with the addition of the if statement – Nathan Anderson Nov 23 '13 at 23:30
  • That line can be before checking fur null. If HDDSelectionBox.SelectedItem is null, it will return an empty list. – Szymon Nov 23 '13 at 23:32
  • so far ive public ListLoad Images{ then new list(); then the if, but it is underlining me on the new List line – Nathan Anderson Nov 23 '13 at 23:48
  • thanks for that, the code runs without error but listbox shows nothing when I select a item from the combobox, It may just be my binding but I have gone through and checked – Nathan Anderson Nov 24 '13 at 02:47
  • Does it return a list of `Photos` when you step through the code? – Szymon Nov 24 '13 at 02:49
  • not that I can see, I'm not sure if its doing anything because I get no errors – Nathan Anderson Nov 24 '13 at 02:56
  • it may be that I need to somehow move this into a event under combobox selectionchanged, because the listbox is only supposed to be filled after a drive has been selected, do correct me if I'm wrong but the list may just be sitting doing nothing seeming as its not under a event – Nathan Anderson Nov 24 '13 at 03:06
  • Yes, that sounds possible – Szymon Nov 24 '13 at 03:15