Skip to content

Fix NPE in ExpandFunctionDefinitionConverter for getDerivedUnits (issue #268)#279

Open
dyrpsf wants to merge 1 commit intosbmlteam:masterfrom
dyrpsf:fix-derived-units-npe
Open

Fix NPE in ExpandFunctionDefinitionConverter for getDerivedUnits (issue #268)#279
dyrpsf wants to merge 1 commit intosbmlteam:masterfrom
dyrpsf:fix-derived-units-npe

Conversation

@dyrpsf
Copy link
Contributor

@dyrpsf dyrpsf commented Jan 30, 2026

Fixes #268.

Calling AssignmentRule.getDerivedUnits() on the rule for variable japl in BIOMD0000000327.xml previously
resulted in a NullPointerException during function definition expansion.

Root cause

  • While expanding function definitions, ExpandFunctionDefinitionConverter.replaceAll(...) walks the AST and,
    for each occurrence of a bound variable bvar, retrieves current.getParent() and casts it directly to ASTNode.
  • In some trees (as seen in BIOMD0000000327.xml when getDerivedUnits() triggers expansion), the parent of the
    matching node is null or not an ASTNode, which leads to an NPE when calling parent.getIndex(current).

Changes

  • Updated replaceAll(ASTNode newMath, ASTNode bvar, ASTNode expandedBVar) to guard against null / non-ASTNode
    parents:

    TreeNode parentNode = current.getParent();
    if (parentNode instanceof ASTNode) {
      ASTNode parent = (ASTNode) parentNode;
      int index = parent.getIndex(current);
      if (index != -1) {
        parent.replaceChild(index, expandedBVar);
      }
    }
  • This keeps the existing behavior for all valid parent–child relationships, but avoids dereferencing null or non-ASTNode parents.

Tests

  1. Added AssignmentRuleDerivedUnitsTest under core/test/org/sbml/jsbml/:
  • Loads the example model BIOMD0000000327.xml (added under core/test/org/sbml/jsbml/testdata/).

  • Locates the AssignmentRule with variable "japl".

  • Asserts that rule.getDerivedUnits() does not throw a NullPointerException.

  1. Ran:
mvn -pl core -Dtest=AssignmentRuleDerivedUnitsTest test
mvn -pl core test

Both complete with BUILD SUCCESS on Java 11.

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.

Calling rule.getDerivedUnits() on Assignmentrule for japl in the biomodel BIOMD0000000327.xml results in java.lang.NullPointerException

1 participant