Null detection
1. Null detection
Now we know how to calculate the spread of our data, but what if some data is missing?2. Where is the data?
Datasets may have two types of missing data: nulls and blank values. Nulls are like empty spots on our bookshelf, and they represent data that's not available. The last row's null values for rating and review count may be missing for a good reason, like a new edition without reviews yet - they are meaningful nulls. Price should always be present, so a null price is an error with missing data. Blank values are like whitespaces in data that could be typos. The second row's blank title likely represents a data entry error, such as accidentally entering a blank space.3. Errors from null values
Let's investigate nulls further and see what happens when the rating is set to null in `BookSales`. When our code attempts to get a null rating with `book.rating()`, Java throws a `NullPointerException`. This is similar to trying to compute the average of a list that has no numbers - there's simply nothing to work with.4. Checking for null values
To prevent these crashes, we must check if values exist before using them. From the `Objects` library, `Objects.isNull()` checks for null values before we try to use them. If `book.rating()` is null, we then print "Missing rating."5. Providing default values for nulls
If a value is null, we can provide default values instead. For numerical values, we can use `Optional.ofNullable()` from the `Optional` library to wrap a potentially null rating and provide 0.0 as a fallback with the `.orElse()` method. Similarly, `Objects.toString()` handles null strings by returning a default value like "[No Title]". Both approaches prevent null errors while providing meaningful default values.6. Checking for blank/null strings
Next let's examine how to check for blanks and nulls in strings. First we import the `StringUtils` library for string validation. Users might accidentally save spaces or empty values, leading to an empty title in our `book`. `StringUtils.isBlank()` detects three common data entry errors: null values, empty strings, and whitespace-only strings - making it more robust than `.isNull()` for strings. If `book.title()` is blank, then the code prints "Invalid - empty title."7. Checking for blank numbers
Before parsing strings into numbers, we check for blanks using `.isBlank()` like we did for strings.8. Summary: detecting nulls and blanks
We've explored two types of missing data: nulls (undefined data) and blanks (empty text). For nulls, we use `Objects.isNull()` to check values and `Optional.ofNullable()` to provide defaults, preventing `NullPointerException`. For blank text, we use `StringUtils.isBlank()` to detect empty strings, nulls, or whitespace-only values that commonly occur in data entry. These methods help us build robust applications that handle missing data gracefully while maintaining data quality.9. Let's practice!
Now it's your turn to handle missing data!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.