I am trying to load an Image in Silverlight, but i keep getting a null pointer exception when i assign it to this.Image1.Source. I’ve looked over similiar stack questions, but none of them solve the problem. My image file is in a folder right next to the xap file.
public MainPage()
{
Uri uri = new Uri("Content/button.png", UriKind.Relative);
var image = new BitmapImage { UriSource = new Uri("Content/button.png", UriKind.Relative) };
try
{
BitmapImage img = new BitmapImage(uri);
this.image1.Source = image;
}
catch (Exception e)
{
string problem = e.Message;
}
InitializeComponent();
}
Ok, i’ve found the problem, i was trying to assign image to image1.Source, but image1 was not yet created – it is created in the InitilizeComponent() function.