• 0 Posts
  • 3 Comments
Joined 2 years ago
cake
Cake day: April 5th, 2024

help-circle
  • NPEs in Java usually have 2 causes and they are easily preventable:

    1. Parsing or deserializing data that must be present but is not. Fix: Add a validator or (introduced in Java 17 - 4 years ago) use records and do simple null validation there
    2. Devs coding weird shit that might or might not return null. Fix: Use annotations like NotNull/Nullable or the Optional wrapper (introduced in Java 8 - 11 years ago). There is also progress underway to be able to explicity declare type nullness, however - as always - Oracle invests more money into it’s lawyers instead of their devs, so it will take some time until this will be available.

    The problem is not really language specific because quite the same can also happen in any programming language, the symptoms/errors are just different.