From d8d8a9539f69d0a120cf6917d2318fa33fbea1c9 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 20 Sep 2015 20:51:09 +0200 Subject: [PATCH] #1409 NullPointerException in InsufficientStringBufferRule --- ...nsufficientStringBufferDeclarationRule.java | 18 +++++++++++------- .../InsufficientStringBufferDeclaration.xml | 13 +++++++++++++ src/site/markdown/overview/changelog.md | 2 ++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java index 4302064375..4c3f75cec3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java @@ -305,13 +305,17 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule { int size = 0; if (parent != null) { final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class); - final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class); - for (int i = 0; i < primExp.jjtGetNumChildren(); i++) { - final Node sn = primExp.jjtGetChild(i); - if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) { - continue; - } - size += processNode(sn); + if (initializer != null) { + final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class); + if (primExp != null) { + for (int i = 0; i < primExp.jjtGetNumChildren(); i++) { + final Node sn = primExp.jjtGetChild(i); + if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) { + continue; + } + size += processNode(sn); + } + } } } return size; diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml index aecf59ed50..cbfbbab0d9 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml @@ -1023,6 +1023,19 @@ public class StringBuilderWithMessageRetrieval { } } +} + ]]> + + + + #1409 NullPointerException in InsufficientStringBufferRule + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 0d1d45717d..b4bc97bc31 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -18,6 +18,8 @@ * java-controversial/DefaultPackage: [#1410](https://sourceforge.net/p/pmd/bugs/1410/): DefaultPackage triggers on field annotated with @VisibleForTesting +* java-strings/InsufficientStringBufferDeclaration: + [#1409](https://sourceforge.net/p/pmd/bugs/1409/): NullPointerException in InsufficientStringBufferRule **API Changes:**