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-why-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
_______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid