Skip to content

[java] NullAssignment: False positive inside if statement for first assignment #1050

Closed

Description

@adangel

Affects PMD Version: 6.3.0+, 7.0.0

Rule: NullAssignment

Description:
If there is a variable declaration without initializer, then the first assignment with null is considered already as a NullAssignment. This should be ignored, since it is the first assignment, which should be allowed by the rule.

Code Sample demonstrating the issue:

String key;
if (a) {
	key = "a";
} else if (b) {
	key = "b";
} else {
	key = null;
}

See #629 for the original issue.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Activity

  1. added
    a:false-positive
    on Apr 25, 2018
  2. boris-petrov commented on Aug 3, 2018

    @boris-petrov
    Contributor

    I think I've found another false-positive for this check (PMD version 6.6.0):

    return new Pair<>(found ? a : null, found ? b : null);

    I think this also could be considered a bug in PMD, what do you think?

  3. changed the title [-] [java] NullAssignment false positive inside if statement[/-] [+] [java] NullAssignment - false positive inside if statement[/+] on Dec 23, 2021
  4. added
    needs:pmd7-revalidation
    and removed
    needs:pmd7-revalidation
    on Mar 17, 2024
  5. changed the title [-] [java] NullAssignment - false positive inside if statement[/-] [+] [java] NullAssignment: False positive inside if statement for first assignment[/+] on Dec 19, 2025
  6. added
    in:data-flow
    on Dec 19, 2025
  7. changed the title [-] [java] NullAssignment: False positive inside if statement for first assignment[/-] [+][java] NullAssignment: False positive inside if statement for first assignment[/+] on May 16, 2026
  8. added a commit that references this issue on Sep 12, 2026
    bfc17f2
  9. added this to the 7.28.0 milestone on Sep 14, 2026
  10. added a commit that references this issue on Sep 14, 2026
    35aef60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivein:data-flow

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions