My.Resources.GetObject doesn't work in this case

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I have about 100 JPG files to use as weather icons, and I want to put them in the resources to get away from having to navigate to a file path to get them. On my forecast page, I finally got it all working, so I tried to change the line where it gets the icon from a file path to My.Resources.GetObject like JohnH showed me. It works from the file path, but not from the resources. Why is this? Here's the code. I'll leave it so that Day 1 tries to get it from My.Resources, and Day 2 gets it from a file path so you can see the difference.
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Day1IconLoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]Day1IconLoc = [/SIZE][SIZE=2][COLOR=#800000]"a"[/COLOR][/SIZE][SIZE=2] & xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/dayf/day[@d='1']/part[@p='d']/icon"[/COLOR][/SIZE][SIZE=2]).InnerText
Day1Pic.Image = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Resources.ResourceManager.GetObject(Day1IconLoc)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Day2IconLoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]Day2IconLoc = [/SIZE][SIZE=2][COLOR=#800000]"a"[/COLOR][/SIZE][SIZE=2] & xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/dayf/day[@d='2']/part[@p='d']/icon"[/COLOR][/SIZE][SIZE=2]).InnerText & [/SIZE][SIZE=2][COLOR=#800000]".jpg"
[/COLOR][/SIZE][SIZE=2]Day2Pic.Image = Image.FromFile([/SIZE][SIZE=2][COLOR=#800000]"C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\X-Weather1\X-Weather1\test\"[/COLOR][/SIZE][SIZE=2] & Day2IconLoc)[/SIZE]
[SIZE=2]
[/SIZE]
 
Blake81, debug your Day1IconLoc variable, make sure the string there is same as one of the resource names.
 
The variable is getting set to something like "a32" like it should. I'm using two sets of weather icons. They're all numbers, but I ran into some problems, so the big set is b+number (like b20) and the small ones are a+number. Does my variable need the ".jpg" at the end? I guess that question is irrelevant anyway, because I tried it with and without .jpg. As for what Arkette said, I'm not sure how to do that, so I probably haven't. Does that matter at this point if I'm just debugging and not running a published version? Let me know how to do that and I'll give it a shot. Thanks.
 
By the way, I've also been getting these weird errors in the Immediate Window as soon as the form loads, and I don't know what they mean. Here's the text of one, and I also sometimes get one about something with System.Drawing.dll or something like that.

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
 
Do you actually have a image in resources with name "a32"? The resource name displays besides/beneath each resource. If so, have you tried loading it to picturebox directly with code:
VB.NET:
picturebox1.image=My.Resources.ResourceManager.GetObject("a32")
You must also be certain that the variable actually is what you expect it to be when an image is not displaying, in other case no resource is found and resource manager returns Nothing.
 
Then you're not getting the data you expect from xml (sometimes?)

I've just loaded that xml address and ran that .SelectSingleNode("/weather/dayf/day[@d='1']/part[@p='d']/icon").InnerText bit a lot of times and it currently always returns string "32". Thus the GetObject("a32") always sets this image from resource.-
 
I'm getting the data I'd expect from the XML, and the variable is getting set to "a32" (or whatever number), but this line doesn't work:

VB.NET:
[SIZE=2]Day1Pic.Image = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Resources.ResourceManager.GetObject(Day1IconLoc)
[/SIZE][SIZE=1]

I'm also seeing the following in the immediate window every time I debug. How would I go about figuring out what is causing this?

A first chance exception of type 'System.NullReferenceException' occurred in X-Weather1.exe
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
[/SIZE]
 
I'd say the SelectSingleNode statement was sometimes returning no node, ie is Nothing and you access the InnerText of it, but that would give a NullReferenceException..
 
Back
Top