本文介绍了如何修复未设置为对象错误实例的对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试从xml文档获取输出,一切正常。但是当我评论(删除SecurityGroup Word元素)时,对于该元素,我得到的对象引用未设置为对象的实例。 我的xml文档 < pre lang = xml > < pre > < 整合 > < 案例 > < CaseCategory > FAM < / CaseCategory > < CaseType Word = DMA > 家庭虐待< / CaseType > < BaseCaseType > 民事家庭暴力< / BaseCaseType > < CaseTitle > X v / s Y < / CaseTitle > < ! - < SecurityGroup Word =CONFPOR> Conf - 保护订单< / SecurityGroup> - > < 法院 > < NodeID > 131 < / NodeID > < / Court > < / Case > < / Integration > 我的vb.net代码 Dim strCaseTypeCodeWord As String Dim strBaseCaseType As String Dim strCaseSecurityGroupCodeWord As String Dim intNodeID As Integer strCaseTypeCodeWord = aobjXMLInputDoc。 DocumentElement.SelectSingleNode( Case / CaseType / @ Word)。InnerText strBaseCaseType = aobjXMLInputDoc .DocumentElement.SelectSingleNode( Case / BaseCaseType)。InnerText strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Case / SecurityGroup / @ Word)。InnerText intNodeID = CInt (aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Assignment / Court / NodeID)。InnerText) 结果 strCaseTypeCodeWord = DMA strBaseCaseType =民用家庭暴力 strCaseSecurityGroupCodeWord这是我将Object引用设置为未设置为对象实例的地方。因为我在xml文档中注释/删除了这个元素。它是一个可选元素。 即使xml文档中不存在此SecurityGroup,我还需要做什么才能使vb.net代码正常工作? 我尝试了什么: Dim strCaseTypeCodeWord As String Dim strBaseCaseType 作为 字符串 Dim strCaseSecurityGroupCodeWord 作为 字符串 Dim intNodeID 作为 整数 strCaseTypeCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / CaseType / @ Word)。InnerText strBaseCaseType = aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / BaseCaseType)。InnerText strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Case / SecurityGroup / @ Word)。InnerText intNodeID = CInt (aobjXMLInputDoc.DocumentElement.SelectSingleNode( 案例/作业/法院/ NodeID)。InnerText) 解决方案 关于你正在调用方法的变量或获取/设置属性值为null。 我们无法告诉您哪个,因为我们没有您的代码使用的数据。 只需使用调试器,您就可以轻松找到导致此问题的原因。当代码停止时,调试器将显示异常中的哪一行。将鼠标悬停在每个变量和对象上以检查其内容。其中至少有一个将为空。然后你必须向后追踪你的代码,直到找到为什么/如何变量/对象得到null值。 将鼠标悬停在每个变量名称上,如 strCaseTypeCodeWord 和object,就像 aobjXMLInputDoc.DocumentElement.SelectSingleNode(Case / CaseType / @ Word)的每一部分一样。 Quote:如何修复未设置为对象错误实例的对象引用? 如果你期望 aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Case / SecurityGroup / @ Word)。InnerText 失败因为节点是可选的,你需要检查保存结果的变量是否仍为字符串或NULL。如果变量为NULL,则设置默认值。 strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Case / SecurityGroup / @ Word)。InnerText if strCaseSecurityGroupCodeWord = NULL 然后 ... end 如果 另一个选项是在访问它之前检查Node是否为null。 strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode( 案例/案例/安全组/ @ Word)!= null ? aobjXMLInputDoc.DocumentElement.SelectSingleNode( Case / Case / SecurityGroup / @ Word)。InnerText:串 .empty I am trying to get output from xml document and everything is working. However when I comment (remove SecurityGroup Word element), for that element I am getting Object reference not set to an instance of an object.My xml document<pre lang="xml"><pre><Integration> <Case><CaseCategory>FAM</CaseCategory><CaseType Word="DMA">Domestic Abuse</CaseType><BaseCaseType>Civil Domestic Violence</BaseCaseType><CaseTitle>X v/s Y</CaseTitle><!--<SecurityGroup Word="CONFPOR">Conf - Protective Order</SecurityGroup>--><Court> <NodeID>131</NodeID></Court> </Case></Integration>My vb.net codeDim strCaseTypeCodeWord As StringDim strBaseCaseType As StringDim strCaseSecurityGroupCodeWord As StringDim intNodeID As IntegerstrCaseTypeCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").InnerTextstrBaseCaseType = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/BaseCaseType").InnerTextstrCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerTextintNodeID = CInt(aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Assignment/Court/NodeID").InnerText)ResultstrCaseTypeCodeWord = DMAstrBaseCaseType = Civil Domestic ViolencestrCaseSecurityGroupCodeWord This is where I am getting the Object reference not set to an instance of an object. because I commented/removed this element in xml document. It is an optional element. What do I need to do so that the vb.net code will work even when this SecurityGroup does not exist in the xml document?What I have tried:Dim strCaseTypeCodeWord As String Dim strBaseCaseType As String Dim strCaseSecurityGroupCodeWord As String Dim intNodeID As Integer strCaseTypeCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").InnerText strBaseCaseType = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/BaseCaseType").InnerText strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText intNodeID = CInt(aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Assignment/Court/NodeID").InnerText) 解决方案 On of the variable you're calling a method or getting/setting a property value on is null.We can't tell you which because we don't have the data your code is using.YOU can easily find out what's causing this just by using the debugger. When the code stops, the debugger will show you which line through the exception. Hover the mouse over each variable and object to inspect it's contents. At least one of them is going to be null. Then you have to trace your code backwards until you find why/how that variable/object got the value null.Hover over each variable name, like strCaseTypeCodeWord, and object, like each part of aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").Quote:How do I fix object reference not set to an instance of an object error?If you expectaobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerTextto fail because the node is optional, you need to check if the variable holding the result is still a string or NULL. if the variable is NULL, set a default value.strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerTextif strCaseSecurityGroupCodeWord = NULL then ...end ifAnother option is to check if the Node is null before access it value.strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word") != null? aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText : string.empty 这篇关于如何修复未设置为对象错误实例的对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 14:23