Prompt
Always handle null values explicitly and consistently throughout your codebase to prevent null pointer exceptions. Follow these guidelines:
- Validate nulls early using Preconditions to provide clear error messages:
// Use explicit null checks with informative messages
Preconditions.checkNotNull(input, "Input cannot be null");
Preconditions.checkArgument(shape != null && shape.length > 0, "Invalid shape: %s", shape);
- Document null behavior clearly in Javadoc comments:
/**
* Creates a buffer in the specified workspace.
* @param workspace Optional memory workspace to define the buffer in, may be null
* (buffer not created in workspace when null)
*/
public DataBuffer createBuffer(DataType dataType, long length, boolean initialize, MemoryWorkspace workspace) {
// Implementation
}
-
Handle nulls at the source rather than requiring downstream methods to handle them, preventing defensive null checks throughout the codebase.
- Use appropriate types for potentially null values from collections:
// Instead of: int numWords = (int) tokenizerConfig.get("num_words"); // Use: Integer numWords = (Integer) tokenizerConfig.get("num_words"); - Consider using annotations like
@NonNullor@Nullableto clearly indicate intent and enable static analysis tools to catch potential null issues.
Following these practices helps prevent null pointer exceptions and makes code more robust and maintainable.