[mono-android] Not catching null reference - why?
bjarke
jbp at itai.dk
Thu Jun 21 07:42:24 UTC 2012
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.
More information about the Monodroid
mailing list