Always handle null values explicitly and consistently throughout your codebase to prevent null pointer exceptions. Follow these guidelines: 1. **Validate nulls early** using Preconditions to provide clear error messages:
Always handle null values explicitly and consistently throughout your codebase to prevent null pointer exceptions. Follow these guidelines:
// 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);
/**
* 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.
// Instead of:
int numWords = (int) tokenizerConfig.get("num_words");
// Use:
Integer numWords = (Integer) tokenizerConfig.get("num_words");
@NonNull
or @Nullable
to 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.
Enter the URL of a public GitHub repository