From bb2a1b4b5d614a40f39ffd86e8f980b577a3f21b Mon Sep 17 00:00:00 2001 From: James Procter Date: Tue, 26 Sep 2023 11:59:08 +0100 Subject: [PATCH] JAL-3858 JAL-4090 avoid NPE when given out of bound column ranges when requesting ContactList --- src/jalview/datamodel/Alignment.java | 4 ++++ test/jalview/datamodel/PAEContactMatrixTest.java | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 514a326..aef2038 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -2069,6 +2069,10 @@ public class Alignment implements AlignmentI, AutoCloseable @Override public ContactListI getContactListFor(AlignmentAnnotation _aa, int column) { + if (_aa.annotations==null || column>=_aa.annotations.length || column<0) + { + return null; + } ContactListI cl = cmholder.getContactListFor(_aa, column); if (cl == null && _aa.groupRef != null) { diff --git a/test/jalview/datamodel/PAEContactMatrixTest.java b/test/jalview/datamodel/PAEContactMatrixTest.java index bdfa5a3..fa7aca5 100644 --- a/test/jalview/datamodel/PAEContactMatrixTest.java +++ b/test/jalview/datamodel/PAEContactMatrixTest.java @@ -116,7 +116,11 @@ public class PAEContactMatrixTest AlignmentI alForSeq = new Alignment(new SequenceI[] { alseq }); newaa = AlignmentUtils.addReferenceAnnotationTo(alForSeq, alseq, newaa, null); - ContactListI alcl = alForSeq.getContactListFor(newaa, 1); + // check for null on out of bounds + ContactListI alcl = alForSeq.getContactListFor(newaa, newaa.annotations.length); + assertNull(alcl,"Should've gotten null!"); + // now check for mapping + alcl = alForSeq.getContactListFor(newaa, 1); assertNotNull(alcl); mappedCl = alcl.getMappedPositionsFor(0, 4); assertNotNull(mappedCl); -- 1.7.10.2