0

I'm currently trying to update a program by having a button (nextImg) that when clicked calls a function that creates two bitmaps, generates a number starting from 0, and increments every time the nextImg button gets clicked. Then the program takes that number(num) and returns a string from a List[num] which has all the Uri sources of my photos. However when I click next image I get a

System.NullReferenceException.

private void Image_Loaded(object sender, RoutedEventArgs e)
    {
        // Create a new BitmapImage.
        BitmapImage b = new BitmapImage();
        BitmapImage b1 = new BitmapImage();
        Image img = new Image();

        string sSrc = numGen();            
        b.BeginInit();
        b.UriSource = new Uri(sSrc);
        b.EndInit();

        b1.BeginInit();
        b1.UriSource = b.UriSource;
        b1.EndInit();

        img = sender as Image;
        img.Source = b;         // Error on this line

        Canvas1.Height = Picturebox1.Height;
        Canvas1.Width = Picturebox1.Width;
        Canvas1.Margin = Picturebox1.Margin;
    }
Kyle Dama
  • 49
  • 4
  • 3
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dmitry Jan 08 '16 at 17:01
  • 5
    Apparently `sender` is not an `Image`. Debug and find out what it is. – dbc Jan 08 '16 at 17:03

0 Answers0