Isn't the normal (= .NET) behaviour to throw an InvalidCastException in this case?
Regards, Joel From: monodroid-boun...@lists.ximian.com [mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Sayed Arian Kooshesh Sent: Thursday, 21 June, 2012 6:56 PM To: Discussions related to Mono for Android Subject: Re: [mono-android] Not catching null reference - why? correct syntax is: if( (reference.Target as Bitmap) ==null) On Thu, Jun 21, 2012 at 2:42 AM, bjarke <j...@itai.dk> wrote: Hey guys I have a list of bitmaps with weak references however sometimes the bitmap is null. This is okay and as expected however my issue is that it does not work doing a check against null. This is my method; public static Bitmap DecodeFile(string _path) { WeakReference reference; var options = new BitmapFactory.Options {InPreferredConfig = Bitmap.Config.Rgb565}; Bitmap image=null; //refs is a dictionary of Dictionary<string, WeakReference> if (refs.TryGetValue(_path, out reference)) { if (reference.IsAlive) { image = (Bitmap)reference.Target; if (image==null) { // image has been garbage collected // remove reference from cache refs.Remove(_path); } else { return image; } } else { // image has been garbage collected // remove reference from cache refs.Remove(_path); } } image = BitmapFactory.DecodeFile(_path, options); reference = new WeakReference(image); refs.Add(_path, reference); return image; } The check if(image==null) is false even though it is null. If I do a image.Equals(null) I get a nullpointer exception instead. So right now I catch the exception instead and handle it - however I really do not like that approach. Any suggestion is well recieved :) Regards Bjarke -- View this message in context: http://mono-for-android.1047100.n5.nabble.com/Not-catching-null-reference-wh y-tp5710468.html Sent from the Mono for Android mailing list archive at Nabble.com. _______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid -- Extreme Knowledge is not something for which he programs a computer but for which his computer is programming him. -Wozniak
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid