[mono-android] Not catching null reference - why?
Sayed Arian Kooshesh
kooshesh at gmail.com
Wed Jun 27 03:05:25 UTC 2012
the as syntax checks to see if it can be properly null, then instead of
throwing an exception (so memory haevy) just returns a null. So beautifyl.
Drunk. Idk. I just love code. Send me your code I will see if I can't draw
my divine being on to it.
On Thu, Jun 21, 2012 at 5:56 AM, Sayed Arian Kooshesh <kooshesh at gmail.com>wrote:
>
> 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-why-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
>
--
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/20120626/23e1503d/attachment-0001.html>
More information about the Monodroid
mailing list