Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,16 @@ public boolean accepts(Object o) {
ASTNode current = (ASTNode) o;

if (current.equals(bvar)) {
ASTNode parent = (ASTNode) current.getParent();
int index = parent.getIndex(current);
TreeNode parentNode = current.getParent();

if (index != -1) {
parent.replaceChild(index, expandedBVar);
// Only attempt replacement if the parent is a non-null ASTNode
if (parentNode instanceof ASTNode) {
ASTNode parent = (ASTNode) parentNode;
int index = parent.getIndex(current);

if (index != -1) {
parent.replaceChild(index, expandedBVar);
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions core/test/org/sbml/jsbml/AssignmentRuleDerivedUnitsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.sbml.jsbml;

import static org.junit.Assert.*;

import java.io.File;
import java.net.URL;

import org.junit.Test;

public class AssignmentRuleDerivedUnitsTest {

/**
* Regression test for issue #268.
* Calling getDerivedUnits() on an AssignmentRule in BIOMD0000000327.xml
* should not throw a NullPointerException.
*/
@Test
public void testGetDerivedUnitsDoesNotThrowNPE() throws Exception {
// Load the example model from test resources
ClassLoader cl = getClass().getClassLoader();
URL url = cl.getResource("org/sbml/jsbml/testdata/BIOMD0000000327.xml");
assertNotNull("Test SBML file not found on classpath", url);

SBMLDocument doc = new SBMLReader().readSBML(new File(url.toURI()));
Model model = doc.getModel();
assertNotNull(model);

// Find the assignment rule for variable "japl"
AssignmentRule targetRule = null;
for (int i = 0; i < model.getRuleCount(); i++) {
Rule r = model.getRule(i);
if (r instanceof AssignmentRule) {
AssignmentRule ar = (AssignmentRule) r;
if ("japl".equals(ar.getVariable())) {
targetRule = ar;
break;
}
}
}

assertNotNull("AssignmentRule for variable 'japl' not found", targetRule);

// The call to getDerivedUnits() must not throw a NullPointerException
try {
targetRule.getDerivedUnits();
} catch (NullPointerException npe) {
fail("getDerivedUnits() must not throw NullPointerException: " + npe);
}
}
}
Loading