Levels of Helpfulness in Helpful Null Pointers

Java introduced Helpful Null Pointers. Let’s take a look at a few versions using this class:

public class Helpful {
  private String name;
  private Helpful(String name) {
    this.name = name.toUpperCase();
  }
  public static void main(String... args) {
     new Helpful(null);
  }
}

Three approaches

First lets’ run this using Java 11. We get the stack trace

Exception in thread “main” java.lang.NullPointerException
at Helpful.(Helpful.java:4)
at Helpful.main(Helpful.java:7)

Well that’s not helpful. I have to go look at line 4 to even have a clue what is going on. Now let’s run it in Java 17. (Because I’m sticking with Long Term Support versions)

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because “<parameter 1>” is null
at Helpful.(Helpful.java:4)
at Helpful.main(Helpful.java:7)

That’s more helpful. It tell us the toUppercase() call is the problem. And that a constructor was the cause. It would be even more helpful to know the name of the variable. This requires a command line flag. Compile with -g:vars and you get this when running

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because “name” is null
at Helpful.(Unknown Source)
at Helpful.main(Unknown Source)

Conveniently, both Eclipse and IntelliJ turn on this flag by default.

zoom new years eve – looking back on a year

Last night was the second year in a row that my friends and I did a Zoom New Years Eve party. It felt very different this year. I’m writing this blog post to reflect on why.

I’m not burned out on zoom

Last year, I had very little in person interaction for the over 9 months leading up until New Years. (In a “good” month, I saw two friends.) Having everything happen on Zoom just made me miss real humans even more. It was like a constant reminder of what I was missing. Now, Zoom is more because it is appropriate. Not everything needs to be in person.

I saw some of them more recently

I saw one of the people at the zoom party in person in early December and four others in August or September. (The remaining person I rarely see outside this annual party in the first place)

I don’t mind spending New Years at home

Granted it’s been a long time, but I have spent New Years Eve alone watching TV. And I’m fine with that. So it’s not a day where I feel sad if I can’t see people. (I do enjoy seeing my friends; it just doesn’t have to be then). Which means last year was more about “ugh, another Zoom thing”

I spend way less time in my apartment

There was definitely a cumulative effect of feeling trapped at home. Now that I’ve been working in the office, I get significant breaks from my apartment. Also, my work problems stay at work and aren’t in my home. This separation has helped me a lot.

Using a Local Record for Readability

Sometimes there is something we really want to put in our cert book that doesn’t fit. You can only have so many case studies/real world scenarios after all. I rescued this from the trash heap and made this blog post./

Suppose we want to count the number of names that have both a first and last name beginning with E. We want to call the method as:

var names = List.of("Sarah Smith", "Eve Edwards");
System.out.println(local.countNamesStartingWithEs(names));

We can read this with streams:

public long countNamesStartingWithEs(List<String> names) {
   return names.stream()
      .map(t -> t.split(" "))
      .filter(a -> a[0].startsWith("E"))
      .filter(a -> a[1].startsWith("E"))
      .count();
}

The code works. However, it has array positions sprinkled throughout. This requires you to keep remembering the format. We can refactor using a local record:

public long countNamesStartingWithEsWithRecords(List<String> names) {
   record Name(String first, String last) {}

   return names.stream()
      .map(t -> t.split(" "))
      .map(t -> new Name(t[0], t[1]))
      .filter(n -> n.first().startsWith("E"))
      .filter(n -> n.last().startsWith("E"))
      .count();
    }

The record lets us give the fields names so we can reference them as n.first() and n.last(). The only place that has to know about the order is the call record constructor. For such a simple example, the approaches are similar. For longer and more complex pipelines, the local record approach can make the code easier to read.