Back to all reviewers

Validate nulls properly

spring-projects/spring-boot
Based on 3 comments
Java

Always use appropriate null validation mechanisms to prevent NullPointerExceptions and ensure code robustness. For string parameters: - Use `Assert.hasText(profile, "Profile must have text")` instead of `Assert.notNull()` when you need to validate both null and empty strings

Null Handling Java

Reviewer Prompt

Always use appropriate null validation mechanisms to prevent NullPointerExceptions and ensure code robustness.

For string parameters:

  • Use Assert.hasText(profile, "Profile must have text") instead of Assert.notNull() when you need to validate both null and empty strings
  • This handles both null checks and empty string validation in one call

For equals methods:

  • Always check for null before comparing class types:
    @Override
    public boolean equals(Object obj) {
      if (this == obj) {
          return true;
      }
      if (obj == null || getClass() != obj.getClass()) {
          return false;
      }
      // Continue with comparison
    }
    

For collections and arrays:

  • Always validate that collection parameters are not null before processing them
  • For optional collections, consider:
    if (resolvers != null) {
      // process resolvers
    }
    
  • Or enforce non-null values through constructor validation

Using these patterns consistently will reduce null-related bugs and improve code quality across the codebase.

3
Comments Analyzed
Java
Primary Language
Null Handling
Category

Source Discussions