2015-09-25 81 views
0

我所试图做的是,找到一个对象名单C#查找对象

Log q = logs.Find(x => (x.dnsq.queryID.Contains(queryid))); 

然而,当我这样做,我得到这个错误

x.dnsq内。 queryID = 'x.dnsq.queryID' 扔 类型的异常“System.NullReferenceException

Log类看起来像这样

class Log : LogCtrl 
{ 
    public int id { set; get; } 
    public string source { set; get; } 
    public string destination { set; get; } 
    public string protocol { set; get; } 
    public double time { set; get; } 
    public string info { set; get; } 
    public DNSresponse dnsr { set; get; } 
    public DNSquery dnsq { set; get; } 
} 
+0

你的对象之一为空或dnsq为空或queryID为null – Steve

回答

2

听起来像x或x.dnsq或x.dnsq.queryId对于日志中的至少一个项目为空。你可以做你的声明检查此如下:

Log q = logs.Find(x => (x != null && x.dnsq != null && x.dnsq.queryID != null && x.dnsq.queryID.Contains(queryid))); 
+0

或用新的'.'操作 –

+0

嗯,我使用的调试?检查日志列表,我知道它包含一个条目,其中dnsq.queryID等于我正在搜索的内容。 –

+1

@Mathias_并不重要,如果列表包含它或没有,只要其中一个字段为空它将抛出空例外 – Steve