简体   繁体   English

如何处理HtmlDocument空异常

[英]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;
    }
}

PIC

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.

相关问题 如果model为null,如何处理null异常,如何在mvc中的视图中处理它? - how to handle null exception if model is null , how to handle it in view in mvc? 如何在C#中处理null异常 - how to handle null exception in C# HTMLDocument加载返回空值 - Htmldocument Load Returns Null 如何处理从 webgrid 中的控制器返回的列值的空异常? - How to handle null exception for column values returned from controller in webgrid? 在何处/如何实例化对象以处理空引用异常 - Where/How to instaniate an object to handle null reference exception 如何使用MySQL DataReader在vc#中处理空指针异常? - How to handle null pointer exception in vc# using MySQL DataReader? 如何处理dataGridView1_SelectionChanged事件中的空异常? - How to handle null exception in dataGridView1_SelectionChanged event? 如何修改getter和setter,以处理空引用异常 - How to modify a getter and setter, to handle a null reference exception 从对象转换为字符串时如何处理空异常? - How to handle null exception when converting from object to string? 如何从WebBrowser控件HtmlDocument获取对HtmlElement的句柄或对象引用? - How can I get a handle or object reference to an HtmlElement from a WebBrowser control HtmlDocument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM