3

I have some Image components on the page and some static images defined in App.xamp. On startup I need to fill Image components with random static images.

int[] squareImageSources = new int[13]; 
Random rnd = new Random();
Image[] squareImages = new Image[13];

public Page1()
{
    squareImages[0] = i0;
    squareImages[1] = i1;
    squareImages[2] = i2;
    squareImages[3] = i3;
    squareImages[4] = i4;
    squareImages[5] = i5;
    squareImages[6] = i6;
    squareImages[7] = i7;
    squareImages[8] = i8;
    squareImages[9] = i9;
    squareImages[10] = i10;
    squareImages[11] = i11;
    squareImages[12] = i12;
    InitializeComponent();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    int randomNumber = rnd.Next(28);
    for (int i = 0; i < 13; i++)
    {
        while (squareImageSources.Contains(randomNumber))
            randomNumber = rnd.Next(28);
        squareImageSources[i] = randomNumber;
        squareImages[i].Source = (BitmapImage)System.Windows.Application.Current.FindResource("s" + Convert.ToString(randomNumber + 1)); //application closes here
    }
}

App.xaml:

<BitmapImage x:Key="s1" UriSource="pack://application:,,,/Resources/Photos/1.png"/>
<BitmapImage x:Key="s2" UriSource="pack://application:,,,/Resources/Photos/2.png"/>
.....................................................
<BitmapImage x:Key="s28" UriSource="pack://application:,,,/Resources/Photos/28.jpg"/>

But application just closes, I recieve no exception. What can be wrong with this code?

UPD:

Tried this way:

try
{
    BitmapImage bi = (BitmapImage)System.Windows.Application.Current.TryFindResource("s" + Convert.ToString(randomNumber + 1));
    squareImages[i].Source = bi; //nullReferenceException
}
catch
{

}

catch catches NullReferenceException. How is that possible? I can use this recource in designer and if works fine.

AndrewR
  • 592
  • 2
  • 6
  • 20
  • Have you enabled Common Language Runtime Exceptions in Debug->Exceptions? Also, consider to enable WPF Tracing via your config-file (rightclick and select to configure might start a wizard - it does for WCF)). It gives details that I don't know how to get otherwise. – hlintrup Mar 18 '16 at 14:55
  • 1
    you are doing quite a bit in that line. Did you try to seperate that line, to verify that the FindResource call is the problem? After you did that you can try to catch the Exception (like here https://msdn.microsoft.com/en-us/library/system.windows.application.findresource(v=vs.110).aspx) and then post the result here. – Florian Mar 18 '16 at 15:07
  • updated the question – AndrewR Mar 18 '16 at 15:18
  • [FindResource](https://msdn.microsoft.com/en-us/library/system.windows.application.findresource(v=vs.110).aspx) may throw `ResourceReferenceKeyNotFoundException`. You are not handling it. If you don't handle [unhandled exceptions](http://stackoverflow.com/q/793100/1997232) as well then application will simply crash. As for `TryFindResource` it may return `null`. You are getting `NullReferenceException` because `squareImages[i]` is not `Image` (but `null`). – Sinatr Mar 18 '16 at 15:18
  • `FindResource()` and `Resorces[]` are giving completely same results – AndrewR Mar 18 '16 at 15:22

1 Answers1

1

When you assign something to

squareImages[i].Source = ...

you do not show where squareImages is defined. And it seems like the value of squareImages[i] is null.

This will throw NullReferenceException in both cases (in first one will crash application).

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • No, that's definitely not the problem. `squareImages` is initialized. Added initialization to the question. – AndrewR Mar 18 '16 at 15:35
  • It **is** the problem. Just set a breakpoint. `Page1` and then suddenly `UserControl_Loaded` is suspicious. – Sinatr Mar 18 '16 at 15:37
  • Thank you! I've placed `squareImages[0] = i0;...` before `InitializeComponent();`, not after. I forgot that `InitializeComponent();` initializes i0. – AndrewR Mar 18 '16 at 15:47