Back to all reviewers

Add proactive null checks

apache/kafka
Based on 3 comments
Other

Always add null checks before accessing methods or properties on objects that can potentially be null, especially when dealing with Java APIs or exceptional conditions like RejectedExecutionException.

Null Handling Other

Reviewer Prompt

Always add null checks before accessing methods or properties on objects that can potentially be null, especially when dealing with Java APIs or exceptional conditions like RejectedExecutionException.

When working with Java collections that return null (unlike Scala collections that return Option), use appropriate null safety patterns:

// Before accessing methods on potentially null objects
if (task != null && !task.isDone) {
  // safe to call methods on task
}

// When working with Java Map.get() which can return null
val response = responses.get(partition)
assertNotNull(response)
result.fire(response)

// Alternative approach using Optional
val response = Optional.ofNullable(responses.get(partition))
assertTrue(response.isPresent)
result.fire(response.get)

This prevents NullPointerException at runtime and makes the code more robust, particularly when integrating Scala code with Java APIs or handling exceptional conditions where objects might not be properly initialized.

3
Comments Analyzed
Other
Primary Language
Null Handling
Category

Source Discussions