c# Object reference not set to an instance of an object
error in Infopath:
Object reference not set to an instance of an 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)
error in VB2005: Object reference not set to an instance of an object. System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an 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)
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.S开发者_JAVA技巧electSingleNode("/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.?
Well, you're dereferencing something that turns out to be 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.
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
- 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? And then further, check all other code paths to determine if elements are null
or returning such.
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
.
Very probably one of your SelectSingleNode() calls returns a null, so you cannot take a .Value off it.
精彩评论