mail-archives/monodroid/2011-December/008151.html

127 строки
4.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [mono-android] MonoDroid: Why would this line throw a NullReferenceException?
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:monodroid%40lists.ximian.com?Subject=%5Bmono-android%5D%20MonoDroid%3A%20Why%20would%20this%20line%20throw%20a%0A%09NullReferenceException%3F&In-Reply-To=">
<META NAME="robots" CONTENT="index,nofollow">
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="008146.html">
<LINK REL="Next" HREF="008154.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[mono-android] MonoDroid: Why would this line throw a NullReferenceException?</H1>
<B>grauenwolf</B>
<A HREF="mailto:monodroid%40lists.ximian.com?Subject=%5Bmono-android%5D%20MonoDroid%3A%20Why%20would%20this%20line%20throw%20a%0A%09NullReferenceException%3F&In-Reply-To="
TITLE="[mono-android] MonoDroid: Why would this line throw a NullReferenceException?">grauenwolf at gmail.com
</A><BR>
<I>Fri Dec 30 16:27:20 EST 2011</I>
<P><UL>
<LI>Previous message: <A HREF="008146.html">[mono-android] Inflate does not display what is expected
</A></li>
<LI>Next message: <A HREF="008154.html">[mono-android] RedLaser bindings for MonoDroid
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#8151">[ date ]</a>
<a href="thread.html#8151">[ thread ]</a>
<a href="subject.html#8151">[ subject ]</a>
<a href="author.html#8151">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>I can't figure out why this line is throwing a null reference exception under
Mono for Android. The same code works fine for WPF, Silverlight, and Windows
Phone.
public PossessionCollection Possessions
{
get { return GetNew&lt;PossessionCollection&gt;(&quot;Possessions&quot;); }
&lt;------
}
The stack trace is showing an error before it gets into the GetNew function.
But for sake of completeness, here it is:
protected T GetNew&lt;T&gt;([CallerMemberNameAttribute] string propertyName)
where T : new()
{
if (propertyName == null)
throw new ArgumentNullException(&quot;propertyName&quot;, &quot;propertyName is null.&quot;);
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException(&quot;propertyName is empty.&quot;, &quot;propertyName&quot;);
Contract.EndContractBlock();
return Properties.GetNew&lt;T&gt;(propertyName);
}
public T GetNew&lt;T&gt;([CallerMemberName] string propertyName)
where T : new()
{
if (propertyName == null)
throw new ArgumentNullException(&quot;propertyName&quot;,
&quot;propertyName is null&quot;);
if (String.IsNullOrEmpty(propertyName))
throw new ArgumentException(&quot;propertyName is empty&quot;,
&quot;propertyName&quot;);
Contract.EndContractBlock();
var result = GetValue(propertyName);
if (result == Missing.Value)
{
result = Activator.CreateInstance&lt;T&gt;();
Set(result, PropertySetModes.SetAsOriginal, propertyName);
}
try
{
return (T)result;
}
catch (NullReferenceException ex)
{
//This can occur if T is a non-nullable value type and the
parent class is mal-formed.
throw new InvalidOperationException(&quot;Property &quot; +
propertyName + &quot; contains a null&quot;, ex);
}
}
Full source code:
<A HREF="http://toonrpg.codeplex.com/SourceControl/list/changesets">http://toonrpg.codeplex.com/SourceControl/list/changesets</A>
<A HREF="http://granite.codeplex.com/SourceControl/list/changesets">http://granite.codeplex.com/SourceControl/list/changesets</A>
--
View this message in context: <A HREF="http://mono-for-android.1047100.n5.nabble.com/MonoDroid-Why-would-this-line-throw-a-NullReferenceException-tp5111089p5111089.html">http://mono-for-android.1047100.n5.nabble.com/MonoDroid-Why-would-this-line-throw-a-NullReferenceException-tp5111089p5111089.html</A>
Sent from the Mono for Android mailing list archive at Nabble.com.
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="008146.html">[mono-android] Inflate does not display what is expected
</A></li>
<LI>Next message: <A HREF="008154.html">[mono-android] RedLaser bindings for MonoDroid
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#8151">[ date ]</a>
<a href="thread.html#8151">[ thread ]</a>
<a href="subject.html#8151">[ subject ]</a>
<a href="author.html#8151">[ author ]</a>
</LI>
</UL>
<hr>
<a href="http://lists.ximian.com/mailman/listinfo/monodroid">More information about the Monodroid
mailing list</a><br>
</body></html>