Permalink
Browse files

Fixed NPE in AtomContainerComparator when container has pseudo

atoms.
  • Loading branch information...
dmak committed Jan 17, 2011
1 parent 53ee4b6 commit 9b0478d50c7b5ca10f77fb01d89329db5fe80625
@@ -37,6 +37,7 @@
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
@@ -160,10 +161,17 @@ else if (bondOrderSum1 < bondOrderSum2)
private double getMolecularWeight(IAtomContainer atomContainer) throws CDKException {
double mw = 0.0;
try {
for (IAtom atom : atomContainer.atoms()) {
if (!atom.getSymbol().equals("H"))
mw += IsotopeFactory.getInstance(atomContainer.getBuilder()).getMajorIsotope(atom.getSymbol()).getExactMass();
}
final IsotopeFactory isotopeFactory = IsotopeFactory.getInstance(atomContainer.getBuilder());
for (IAtom atom : atomContainer.atoms()) {
if (!atom.getSymbol().equals("H")) {
final IIsotope majorIsotope = isotopeFactory.getMajorIsotope(atom.getSymbol());
if (majorIsotope != null && majorIsotope.getExactMass() != null) {
mw += majorIsotope.getExactMass().doubleValue();
}
}
}
} catch (IOException e) {
throw new CDKException(e.getMessage(), e);
}
@@ -28,9 +28,12 @@
import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.PseudoAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IRing;
@@ -65,6 +68,22 @@ public void testCompare_Null_Null() {
Assert.assertEquals("null <-> null", 0, comparator.compare(null, null));
}
@Test
public void testCompare_Atom_PseudoAtom() {
// Instantiate the comparator
Comparator<IAtomContainer> comparator = new AtomContainerComparator();
IAtomContainer atomContainer1 = new AtomContainer();
atomContainer1.addAtom(new Atom("C"));
IAtomContainer atomContainer2 = new AtomContainer();
atomContainer2.addAtom(new PseudoAtom("*"));
Assert.assertEquals(atomContainer1 + " <-> " + atomContainer2, 1, comparator.compare(atomContainer1, atomContainer2));
}
@Test
public void testCompare_IAtomContainer_Null() {
IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();

0 comments on commit 9b0478d

Please sign in to comment.