0

Simply put...

private void LoadImage(object sender, EventArgs e){
    String path = null;
    Microsoft.Win32.OpenFileDialog oFD = new ...();
    if(oFD.showDialog == true){
        path = oFD.FileName;
        result = new BitmapImage(new Uri(path));
        Array.Resize(ref frames, frames.Length + 1);
        frames.Append<BitmapSource>(result);
    }
    Refresh();
}
private void Refresh(){
    BitmapSource bg = frames[curFrame]; //i.e. curFrame = 0;
}

I expected bg to not be null when Refresh() is called. I caught all relevant exceptions except for bg being null, in which I don't want bg to be null when the program is executing.

JoseAyeras
  • 75
  • 7
  • 1
    Why don't you use a `List` instead of a array and `Array.Resize` (which is odd)? – Clemens Apr 01 '19 at 18:23
  • I have issues mixing paradigms sorry. Also this question is closing soon because I found that array.append was the problem that caused me grief. – JoseAyeras Apr 01 '19 at 18:26
  • More broadly, you should have put a breakpoint in your LoadImage() method, and stepped through it at runtime, checking what's in `frames` at each step. You should certainly use List as Clemens suggests, but there was nothing about the old code you couldn't have fixed just by identifying exactly where and how things went wrong. – 15ee8f99-57ff-4f92-890c-b56153 Apr 01 '19 at 18:29
  • Simply put, set frames[length - 1] to result and I get the thing I wanted. And yes, I did put a breakpoint in LoadImage() and step through it, otherwise I would not have even asked the question in the first place because in reality my code has many more lines. – JoseAyeras Apr 01 '19 at 18:30

1 Answers1

0

Calling Array.Append(object) was the cause of my problem in this situation. I don't know what it was for but I assumed (wrongly) that it appends an object to the end of any array.

I replaced that line with the following and my code functions as I intended it to.

frames[frames.Length - 1] = result;
JoseAyeras
  • 75
  • 7
  • Far better: declare `frames` as resizable collection: `private List frames = new List();`. Then just call `frames.Add(result);` – Clemens Apr 01 '19 at 18:44