While Loop throws NullReferenceException


In a project I’m working on, I’m automating an old VB6 application. The particular sequence I’m working on does the following:

GetAttribute -> Retrieve “aastate” attribute from button, store in variable called “aastate” (GenericValue)
MessageBox -> "Delete box aastate: " & aastate.ToString -> “Delete box aastate: focusable”
While -> NOT aastate.ToString.Contains("unavailable")

The first operation inside of the while loop is a message box displaying that we’ve entered the while loop, though this is not hit.

I’ve attached a breakpoint to the while loop and stepped into it. First step is alright, though I’m not sure what this step represents. Stepping in again, I get this message box:


An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , ActivityContext )
   at Microsoft.VisualBasic.Activities.VisualBasicValue`1.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity`1.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

The NRE exception details are rather sparse, though I’m not particularly certain what’s causing it.

I’ve confirmed that aastate is not null (see workflow overview) and to take this a step further, I even replicated the condition for the while loop in a text box. You can see the results of that here:


Upon hitting that while loop though, the NRE is thrown. Here is the xml source for that while loop.

<ui:MessageBox Caption="{x:Null}" ChosenButton="{x:Null}" Buttons="Ok" DisplayName="Message Box" sap:VirtualizedContainerService.HintSize="775,59" sap2010:WorkflowViewState.IdRef="MessageBox_11" Text="[&quot;Delete box aastate: &quot; &amp; aastate.ToString]" TopMost="True" />
<While sap:VirtualizedContainerService.HintSize="775,759" sap2010:WorkflowViewState.IdRef="While_4">
		<Variable x:TypeArguments="ui:GenericValue" Name="aastate" />
	<While.Condition>[NOT aastate.ToString.Contains("unavailable")]</While.Condition>
	<!-- Snip -->

Everything appears to be correct, though if I’ve missed anything, do let me know.

Things I’ve tried:

  1. Messagebox debugging
  2. Debug mode inspection
  3. Adding debug symbol path environment variable
    symbol path (Credit to Tanner of Microsoft for thinking of this, even if nothing happened as a result.)
  4. Inspected published nuget package to see if there were an exe - Tanner wanted to try devenv /debugexe <full path to exe> though unfortunately the nuget package merely contained the project xaml and supporting files.

Additional goodies:

Tanner suggested the following:

Also, at least looking at: https://referencesource.microsoft.com/#System.Activities/Microsoft/VisualBasic/Activities/VisualBasicValue.cs,e591763045cb44cf,references It looks like the code is “probably” going down expressionTree.Compile() and then compiledExpression(context) where compiledExpression is the thing with the NRE

So, it’s likely something in the LINQ expression tree that has the bug

(which I assume is produced from the XAML)

If there’s anything I missed, do let me know. This is a rather strange issue.

As a work-around, I’ll allocate another variable and store the result of the calculation inside that variable, then pass that variable directly into the while loop. Should this not work for whatever reason, I’ll update this post.


Intside the message box, try with the following code. The Convert.ToString will handle null exception. That is in your case if the aastate does not hold any values it will throw the error as you shown, if you use Convert.ToString the aastate null value will be converted as String.Empty value, so no exception will occur. But you need to handle it.

"aatate is available?: " & Not Convert.ToString(aastate).ToLower.Contains("unavailable")

I can give this a try.

Though as far as I can tell, aastate is an enum (inside of a GenericValue). According to Tanner,

enum.ToString()) will never return null as we either get the name or we fall back to printing the raw value of the backing field.

I’ll give it a try and update this post with the result.

So, seems I’ve found the issue.

There was a duplicate aastate value inside of the while loop scope. It had not been initialized yet, but apparently the while loop went inside of its own scope to get the aastate value instead of using the one it had been provided. This duplicate came as a result of moving code blocks around.

Variables/Arguments always use the most local scope possible to determine it’s value, which is why it’s recommended to check and double check that you aren’t using the same names in your workflows, as it’s very easy for bugs like this to occur

Strange behavior at best. I’ll open another thread to address this specifically.

That is normal behavior and completely expected and is true of all programming I’ve ever done. Local takes precedence over global in all cases

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.