Skip to content

Fix NullPointerException in AbstractTreeNode used by ASTNode clone#271

Merged
draeger merged 1 commit intosbmlteam:masterfrom
dyrpsf:astnode-npe-fix
Mar 11, 2026
Merged

Fix NullPointerException in AbstractTreeNode used by ASTNode clone#271
draeger merged 1 commit intosbmlteam:masterfrom
dyrpsf:astnode-npe-fix

Conversation

@dyrpsf
Copy link
Contributor

@dyrpsf dyrpsf commented Jan 6, 2026

This PR fixes a NullPointerException that could occur when cloning AbstractTreeNode (and thus ASTNode) when userObjects is null.

Previously, the copy constructor in AbstractTreeNode did:

if (anode.isSetUserObjects()) {
userObjects = new HashMap<Object, Object>();
userObjects.putAll(anode.userObjects);
}

If a subclass overrides isSetUserObjects() (e.g. ASTNode) and returns true even when the underlying userObjects map is still null, putAll throws an NPE.

The constructor is now changed to check the field directly:

if (anode.userObjects != null && !anode.userObjects.isEmpty()) {
userObjects = new HashMap<Object, Object>(anode.userObjects);
}

This makes the clone logic robust to such overrides.
I verified the fix by:

Building JSBML and running its tests locally.
Building SBSCL 2.1.1 against this JSBML snapshot.
Running the SBMLTestSuite wrapper used in draeger-lab/SBSCL#74 without encountering the previous NPE in ASTNode.clone.
Related issues:

Fixes #258
Related to draeger-lab/SBSCL#74

@draeger
Copy link
Member

draeger commented Jan 6, 2026

I am wondering if this is actually a bug in JSBML? If so, shouldn't it be fixed there?

@luciansmith
Copy link
Member

This is jsbml? Unless I'm missing something?

@draeger
Copy link
Member

draeger commented Jan 6, 2026

This is jsbml? Unless I'm missing something?

Yes, there were several edits on SBSCL. I'll check this PR here, of course.

@draeger draeger self-requested a review March 11, 2026 22:01
@draeger draeger added the bug label Mar 11, 2026
Copy link
Member

@draeger draeger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted.

@draeger draeger merged commit 4788407 into sbmlteam:master Mar 11, 2026
@dyrpsf
Copy link
Contributor Author

dyrpsf commented Mar 12, 2026

Thank you for merging this!
If any further changes or adjustments are needed, please let me know.

@dyrpsf dyrpsf deleted the astnode-npe-fix branch March 12, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException in ASTNode

3 participants