foreach (string s in myField.getChilds()) {
if (s == null)
//handle null
else
//handle normal value
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的程序时,我得到一个NullReferenceException,因为getChilds可能返回null.如何使程序继续运行并处理异常?我不能在foreach之外处理它,无法解释为什么因为它需要花费太多时间(而且我相信你们很忙:P).有任何想法吗?
我已经尝试过这种方式:
foreach (string s in myField.getChilds() ?? new ArrayList(1)) {
if (s == null)
//handle null
else
//handle normal value
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,程序只是跳到foreach的末尾,但我想让它进入foreach而不是!
我正在基类中实现INotifyPropertyChanged,如下所示:
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
var propChangedHandler = PropertyChanged;
if (propChangedHandler != null)
{
var args = new PropertyChangedEventArgs(propertyName);
propChangedHandler(this, args);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用它如下:
RaisePropertyChanged("Name");
Run Code Online (Sandbox Code Playgroud)
我得到一个NullReferenceException,而参数,"this"和处理程序是非null.任何人都可以对此有所了解吗?
谢谢.
- >例外的完整堆栈跟踪:http://pastebin.com/bH9FeurJ
更新当我覆盖包含此属性的类的实例时发生异常.简化示例:
public class Person : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
// More properties etc.
}
Run Code Online (Sandbox Code Playgroud)
-snip-
public …Run Code Online (Sandbox Code Playgroud) 我正在寻找解决DefaultIfEmpty()扩展方法在LINQ外连接中使用时不获取空值的问题的解决方案.
代码如下:
var SummaryLossesWithNets = (from g in SummaryLosses
join n in nets
on g.Year equals n.Year into grouping
from x in grouping.DefaultIfEmpty()
select new
{
Year = g.Year,
OEPGR = g.OccuranceLoss,
AEPGR = g.AggregateLoss,
OEPNET = ((x.OEPRecovery == null) ? 0 : x.OEPRecovery),
AEPNET = ((x.AEPRecovery == null) ? 0 : x.AEPRecovery),
});
Run Code Online (Sandbox Code Playgroud)
在List SummaryLosses中,我希望加入到表'nets'中有多年的数据,其中包含多年的子部分.假设x是一个空值,我假设是因为SummaryLosses中的年份与网络中的年份不匹配会在分组列表中创建空值.
如何在这里检查空值?正如您所看到的,我试图在x的属性上检查null,但由于x为null,因此不起作用.
亲切的问候Richard
我想List用我的自定义类包装类.至于现在我有这样的事情;
public class PriorityListOfNodes
{
private List<Node> list_;
private IComparer<Node> sorter_;
public List<Node> List_ {
get {return list_;}
set {list_ = value;}
}
public PriorityListOfNodes ()
{
sorter_ = new NodeSorter_fValueDescending ();
}
public Node PopFromEnd ()
{
Node temp = new Node (list_ [list_.Count - 1]);
list_.RemoveAt (list_.Count - 1);
return temp;
}
public Node PeekFromEnd ()
{
return list_ [list_.Count - 1];
}
public void Add (ref Node toAdd)
{
Debug.Log (toAdd);
list_.Add (toAdd);
list_.Sort (sorter_); …Run Code Online (Sandbox Code Playgroud) Resharper希望我改变这段代码:
foreach (var item in cmbxColor1.Items)
{
cmbxColor2.Items.Add(item);
. . .
Run Code Online (Sandbox Code Playgroud)
...因为它说,"可能'System.NullReferencesException'"
所以它应该是这样的:
foreach (var item in cmbxColor1.Items)
{
if (null != cmbxColor2.Items)
{
cmbxColor2.Items.Add(item);
. . .
Run Code Online (Sandbox Code Playgroud)
?
我不明白 - 除非null ==空,组合框的项目怎么可能为null?如果null ==为空,那么这就是他们[s,w]调用此代码时应该是什么.
我正在尝试处理NullReference异常,但我很困惑如何处理它.这是我的示例代码,其中引发了NullReference异常:
private Customer GetCustomer(string unformatedTaxId)
{
return loan.GetCustomerByTaxId(new TaxId(unformatedTaxId));
}
Run Code Online (Sandbox Code Playgroud)
现在我用以下方法处理这个问题
public void ProcessApplicantAddress(ApplicantAddress line)
{
try
{
Customer customer = GetCustomer(line.TaxId);
//if (customer == null)
//{
// eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(line.TaxId).Masked));
// return;
//}
Address address = new Address();
address.AddressLine1 = line.StreetAddress;
address.City = line.City;
address.State = State.TryFindById<State>(line.State);
address.Zip = ZipPlusFour(line.Zip, line.ZipCodePlusFour);
}
catch(NullReferenceException e)
{
//eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个简单的代码来检索当前URL的字符串,如下所示:
string currentURL = HttpContext.Current.Request.Url.ToString();
Run Code Online (Sandbox Code Playgroud)
但是,我在运行代码时遇到错误: Object reference not set to an instance of an object.
我假设我必须创建一个HttpContext的实例.HttpContext的参数是HttpContext(HttpRequest request, HttpResponse response)或者HttpContext(HttpWorkerRequest wr).
是否有文档详细说明如何使用这些参数?我对C#很新,所以我不完全确定如何正确地实例化这个对象,并且没有找到任何有用的资源(包括MS库).
我有一个.NET 4.0 Web项目,并使用Microsoft.Bcl.Async来支持异步/等待功能.我注意到在生产环境中,IIS工作进程由于引发了NullReferenceException而不断崩溃System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1().似乎异常在我的代码中的某个地方被抛出并且在AsyncServices.ThrowAsync()方法中被重新抛出.不幸的是,任何异常细节(包括原始堆栈跟踪)都会丢失,因此我不知道最初抛出异常的位置.此外,我试图使用AppDomain.CurrentDomain.UnhandledException和TaskScheduler.UnobservedTaskException事件捕获此异常,但这些事件永远不会被触发.
我有所有错误详细信息(来自EventLog):
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace: at System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Run Code Online (Sandbox Code Playgroud)
我的代码中没有明显的位置可能会抛出此异常并在没有任何输出的情况下处理崩溃.
我该如何调试?有没有办法找到这个例外的来源?
我有一些看起来像这样的记录:
[<DataContract>]
type Rec1 = {
[<DataMember>] mutable field1 : int;
[<DataMember>] mutable field2 : string;
}
[<DataContact>]
type Rec2 = {
[<DataMember>] mutable field3 : Rec1;
[<DataMember>] mutable field4 : int;
}
Run Code Online (Sandbox Code Playgroud)
我DataContactJsonSerializer用来将JSON反序列化为这个结构.这是一个有效的JSON值:
{ "field3": null, "field4": 1 }
Run Code Online (Sandbox Code Playgroud)
这意味着在运行时,field3是null/Unchecked.defaultOf<_>.在Visual Studio 2010中,此测试工作正常:
(deserialize<Rec2> "{ field3: null, field4: 1 }") = { field3 = Unchecked.defaultOf<_>; field4 = 1 } //true
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2013中,相同的代码抛出NullReferenceException:
at Rec2.Equals(Rec2 obj)
Run Code Online (Sandbox Code Playgroud)
偷看ILSpy中的代码,我看到这是生成的:
if(this != null)
{
return …Run Code Online (Sandbox Code Playgroud) 我尽可能地删除了我的应用程序以找到这个问题.仍然看起来:
11-12 15:56:27.908 I/MonoDroid(17629): System.NullReferenceException: Object reference not set to an instance of an object.
11-12 15:56:27.908 I/MonoDroid(17629): at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page page, System.Int32 width, System.Int32 height) [0x0005d] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:279
11-12 15:56:27.908 I/MonoDroid(17629): at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00003] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:196
11-12 15:56:27.908 I/MonoDroid(17629): at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x0000e] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73
11-12 15:56:27.908 I/MonoDroid(17629): at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, …Run Code Online (Sandbox Code Playgroud)