[mono-android] Not catching null reference - why?
Joel Low
joel at joelsplace.sg
Wed Jun 27 01:03:59 UTC 2012
Isn't the normal (= .NET) behaviour to throw an InvalidCastException in this
case?
Regards,
Joel
From: monodroid-bounces at lists.ximian.com
[mailto:monodroid-bounces at 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 <jbp at 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monodroid/attachments/20120627/af4376af/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 6547 bytes
Desc: not available
URL: <http://lists.ximian.com/pipermail/monodroid/attachments/20120627/af4376af/attachment-0001.bin>
More information about the Monodroid
mailing list