Skip to content

Fix NPE in ParticleMathMapping for non-mass-action kinetics#1671

Open
jcschaff wants to merge 1 commit intomasterfrom
fix-particle-math-mapping-mafunc-npe
Open

Fix NPE in ParticleMathMapping for non-mass-action kinetics#1671
jcschaff wants to merge 1 commit intomasterfrom
fix-particle-math-mapping-mafunc-npe

Conversation

@jcschaff
Copy link
Copy Markdown
Member

Summary

  • One-line guard fix in ParticleMathMapping.refreshMathDescription: NPE when a reaction's rate law cannot be reduced to mass-action form.
  • transformToStochastic returns a GeneralKineticsStochasticFunction (sibling of MassActionStochasticFunction) for kinetics like Hill / Michaelis-Menten. The existing instanceof check correctly leaves maFunc null, but the next line dereferenced it unconditionally → NPE far from the real cause.
  • Now throws the same descriptive MappingException (which names the offending reaction) whether maFunc is null or has both rates null.

Why now

Identified during triage of a user-support-email log corpus: this NPE is the second-largest cluster of distinct-incident failures (multiple incidents across all sampled builds, multiple distinct user models). The error users currently see is a stack-trace bottoming out at ParticleMathMapping.java:732 rather than a message naming their reaction.

Diff

// vcell-core/.../ParticleMathMapping.java:729-739
 if (stochasticFunction instanceof MassActionStochasticFunction)
 {
     maFunc = (MassActionStochasticFunction)stochasticFunction;
 }
-if ((maFunc.forwardRate() == null && maFunc.reverseRate() == null))
+// transformToStochastic falls back to GeneralKineticsStochasticFunction
+// when the rate law can't be reduced to mass-action form (e.g. Hill /
+// Michaelis-Menten kinetics). Particle math mapping requires mass-action
+// shape, so report the same error in either case rather than NPE.
+if (maFunc == null || (maFunc.forwardRate() == null && maFunc.reverseRate() == null))
 {
     throw new MappingException("Cannot generate stochastic math mapping for the reaction:" + reactionStep.getName() + ...);
 }

Test plan

  • mvn compile test-compile -pl vcell-core -am
  • mvn test -pl vcell-core -Dgroups="Fast" — pre-existing VCellDataTest.test_3D Poetry environmental error is unrelated to this Java change
  • Manual: open a stochastic application with a non-mass-action reaction (Michaelis-Menten / Hill) and confirm the error message names the reaction instead of producing an NPE

🤖 Generated with Claude Code

…tics

`transformToStochastic` returns a `GeneralKineticsStochasticFunction`
when a reaction's rate law cannot be reduced to mass-action form (e.g.
Hill / Michaelis-Menten kinetics). The existing `instanceof
MassActionStochasticFunction` check correctly leaves `maFunc` null in
that case, but the next line dereferenced it unconditionally
(`maFunc.forwardRate()`), producing an NPE far from the real cause.

Particle math mapping requires mass-action shape regardless, so the
correct behavior is to throw the same `MappingException` whether
`maFunc` is null or has both rates null. Adding `maFunc == null ||`
to the guard yields a descriptive error pointing at the offending
reaction instead of an NPE.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jcschaff jcschaff requested a review from danv61 April 30, 2026 18:58
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.

2 participants