From d2721bd6e858f6b55a4b66733b0a039e144df49e Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 14 Jun 2024 11:50:38 -0400 Subject: [PATCH] Fix NPE in OverriddenAssignmentFixCore moveDown method (#1457) - fixes #1454 --- .../corext/fix/OverriddenAssignmentFixCore.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/OverriddenAssignmentFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/OverriddenAssignmentFixCore.java index 6a4d6543463..a403e4fae2b 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/OverriddenAssignmentFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/OverriddenAssignmentFixCore.java @@ -210,7 +210,16 @@ private void moveDown(final CompilationUnitRewrite cuRewrite, TextEditGroup grou Expression rhs= overridingAssignment.getRightHandSide(); String rhsText= cu.getBuffer().getText(rhs.getStartPosition(), extendedEnd(cuRewrite.getRoot(), overridingAssignment.getParent()) - rhs.getStartPosition()); - String declarationText= cu.getBuffer().getText(declaration.getStartPosition(), fragment.getInitializer().getStartPosition() - declaration.getStartPosition()); + String declarationText= ""; //$NON-NLS-1$ + if (fragment.getInitializer() != null) { + declarationText= cu.getBuffer().getText(declaration.getStartPosition(), fragment.getInitializer().getStartPosition() - declaration.getStartPosition()); + } else { + declarationText= cu.getBuffer().getText(declaration.getStartPosition(), fragment.getStartPosition() + fragment.getLength() - declaration.getStartPosition()); + Hashtable options= JavaCore.getOptions(); + String spaceBeforeAssignment= options.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR) == JavaCore.INSERT ? " " : ""; //$NON-NLS-1$ //$NON-NLS-2$ + String spaceAfterAssignment= options.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR) == JavaCore.INSERT ? " " : ""; //$NON-NLS-1$ //$NON-NLS-2$ + declarationText= declarationText + spaceBeforeAssignment + "=" + spaceAfterAssignment; //$NON-NLS-1$ + } String targetText= declarationText + rhsText; ASTRewrite astRewrite= cuRewrite.getASTRewrite(); ASTNode replacementNode= astRewrite.createStringPlaceholder(targetText, ASTNode.VARIABLE_DECLARATION_STATEMENT);