简体   繁体   English

c# Object 参考未设置为 object 的实例

[英]c# Object reference not set to an instance of an object

error in Infopath:信息路径中的错误:

Object reference not set to an instance of an object. Object 引用未设置为 object 的实例。 at InfoPathFormTemplate5.FormCode.CTRL407_5_Clicked(Object sender, ClickedEventArgs e) at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)在 Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent) 在 Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) 的 InfoPathFormTemplate5.FormCode.CTRL407_5_Clicked(Object sender, ClickedEventArgs e)

error in VB2005: Object reference not set to an instance of an object. VB2005 中的错误:Object 引用未设置为 object 的实例。 System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object."用户代码未处理 System.NullReferenceException 消息="对象引用未设置为 object 的实例。" Source="InfoPathFormTemplate5" StackTrace: at InfoPathFormTemplate5.FormCode.CTRL407_5_Clicked(Object sender, ClickedEventArgs e) in C:\Documents and Settings\pebabczu\Desktop\IntakeForm Copy\InfoPathFormTemplate5\FormCode.cs:line 206 at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent) Source="InfoPathFormTemplate5" StackTrace: 在 InfoPathFormTemplate5.FormCode.CTRL407_5_Clicked(Object sender, ClickedEventArgs e) 在 C:\Documents and Settings\pebabczu\Desktop\IntakeForm Copy\InfoPathFormTemplate5\FormCode.cs:line 206 在 Microsoft.Office.InfoPath。 Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent) 上的 Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)

Code:代码:

string TeamL = xnMyForm.SelectSingleNode("/my:myFields/my:field149", ns).Value;
string ACC = xnMyForm.SelectSingleNode("/my:myFields/my:Bank", ns).Value;
string remarkmain = xnMyForm.SelectSingleNode("/my:myFields/my:field104", ns).Value;
string RemarkHR = xnMyForm.SelectSingleNode("/my:myFields/my:Remarks1", ns).Value;
string RemarkTL = xnMyForm.SelectSingleNode("/my:myFields/my:field55", ns).Value;
string RemarkIT = xnMyForm.SelectSingleNode("/my:myFields/my:RemarksICT", ns).Value;
string Rmain = "Remarks: " + remarkmain;
string RHR = "Remarks: " + RemarkHR;
string RTL = "Remarks: " + RemarkTL;
string RIT = "Remarks: " + RemarkIT;

I dont get it... Just getting into C# what did I do wrong.?我不明白...刚进入 C# 我做错了什么。?

Well, you're dereferencing something that turns out to be null.好吧,您正在取消引用原来是 null 的东西。

It's hard to say exactly which line of code is problematic out of what you posted, but you should look at line 206. I strongly suspect that SelectSingleNode is returning null for one of your XPath expressions - you're then trying to access the Value property, which is causing the exception.很难确切地说出您发布的代码中哪一行有问题,但您应该查看第 206 行。我强烈怀疑SelectSingleNode正在为您的 XPath 表达式之一返回null - 然后您尝试访问Value属性,这导致了异常。

So, you need to:因此,您需要:

  • Work out which line is causing the problem找出导致问题的线路
  • Find out whether it's because the data is wrong or your XPath query找出是因为数据错误还是您的 XPath 查询
  • Consider putting in a nullity check before accessing the value (if you should really, really always find a node, then an exception may well be the right approach already)考虑在访问值之前进行空值检查(如果你真的应该总是找到一个节点,那么异常可能已经是正确的方法了)

Something is being used before it has been instantiated, or is otherwise null - is xnMyForm instantiated?在实例化之前正在使用某些东西,或者是null - xnMyForm实例化了吗? And then further, check all other code paths to determine if elements are null or returning such.然后进一步检查所有其他代码路径以确定元素是否为null或返回此类。

Either xnMyForm is null , or you are trying to select a node that doesn't exist in the document (using SelectSingleNode ) and the .Value is invoked on the returned null . xnMyFormnull ,或者您正在尝试 select 文档中不存在的节点(使用SelectSingleNode )并且在返回的null上调用.Value

Very probably one of your SelectSingleNode() calls returns a null, so you cannot take a.Value off it.很可能您的 SelectSingleNode() 调用之一会返回 null,因此您不能将 a.Value 去掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM