Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

The math_change block throws when operating on undeclared or null variables, inconsistent with variables_set and text_append which auto-initialize.

Changes

  • MathChange.cs: Add null-safe checks before casting, check root context for scope resolution, initialize undeclared/null variables to 0.0
  • MathTests.cs: Add tests for undeclared and null variable cases

Example

Before:

if (context.Variables.ContainsKey(variableName))
{
  var value = (double)context.Variables[variableName];  // NullReferenceException if value is null
  // ...
}
else
{
  throw new ApplicationException($"variable {variableName} not declared");
}

After:

if (variables.ContainsKey(variableName) && variables[variableName] != null)
{
  value = (double)variables[variableName];
}
else
{
  var rootContext = context.GetRootContext();
  if (rootContext.Variables.ContainsKey(variableName) && rootContext.Variables[variableName] != null)
  {
    value = (double)rootContext.Variables[variableName];
    // Update in root context and return
  }
  else
  {
    value = 0.0;  // Initialize to default numeric value
  }
}

Handles both variables declared in XML but never initialized, and variables explicitly set to null by blocks like ControlsFor.

Original prompt

This section details on the original issue you should resolve

<issue_title>ApplicationException when undeclared variable is used</issue_title>
<issue_description>The block "math_change" worked not as aspected.

Here is the error message:
image

Here is my Example xml:
TestResult.txt

I get the error message when i run your unit test with this formular, too.

Originally posted by @Amboryus in #53 (comment)</issue_description>

Comments on the Issue (you are @copilot in this section)

@richorama Thank you for posting this error. I believe this is because the 'TestResult' variable is not declared in your blockly program. This is a case that IronBlock currently doesn't handle. I will add support for it. @richorama hmm, I wonder if it's a scope problem - I'll continue to investigate...

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix ApplicationException when using undeclared variable Fix NullReferenceException in math_change block with undeclared variables Nov 20, 2025
Copilot AI requested a review from richorama November 20, 2025 12:46
@richorama richorama marked this pull request as ready for review November 20, 2025 13:05
@richorama richorama merged commit 16cbb84 into master Nov 20, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ApplicationException when undeclared variable is used

2 participants