[英]How to handle HtmlDocument null exception
I am checking a HtmlDocument() by html id called shippingMessage_ftinfo_olp_1
but problem is that i am unable to check if this is a null exception. 我正在通过一个名为
shippingMessage_ftinfo_olp_1
的html id检查HtmlDocument(),但问题是我无法检查这是否为空异常。 Because when i set if !=null
still it throws exception. 因为当我设置if
!=null
它仍然引发异常。 Anyone can tell me how can i check it if its null without this exception? 任何人都可以告诉我如何在没有此异常的情况下检查它是否为null?
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException:'对象引用未设置为对象的实例。
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(response);
string gerLang = "";
if (htmlDoc.GetElementbyId("shippingMessage_ftinfo_olp_1").InnerText != null)
{
gerLang = htmlDoc.GetElementbyId("shippingMessage_ftinfo_olp_1").InnerText;
if(gerLang.Contains("AmazonGlobal Express-Zustellung"))
{
_outOfStock = false;
}
}
Use a null conditional operator : 使用空条件运算符 :
if (htmlDoc.GetElementbyId("shippingMessage_ftinfo_olp_1")?.InnerText != null)
If htmlDoc
can be null, also change that to htmlDoc?.GetEle....
如果
htmlDoc
可以为null, htmlDoc
其更改为htmlDoc?.GetEle....
Reasoning: A null conditional operator short-circuits the evaluation if the object being evaluated is null, preventing you from getting an exception, in favour of evaluating to null. 推理:如果要评估的对象为空,则空条件运算符会使评估短路,从而防止您得到异常,而倾向于评估为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.