Jump to content

murphybeck

Members
  • Posts

    1
  • Credits

  • Joined

  • Last visited

  • Feedback

    0%

About murphybeck

Profile Information

  • Gender
    Male

murphybeck's Achievements

Newbie

Newbie (1/16)

0

Reputation

  1. NullPointerExceptions are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException. For example, String s = null; int len = s.length(); // NullPointerException because s is null So you should check if the variable is null before calling any method on it, for example: int len; if (s == null) { len = 0; } else { len = s.length(); // safe, s is never null when you get here } How to avoid NullPointerExceptions? Use the final modifier to enforce good initialization. Avoid returning null in methods, for example returning empty collections when applicable. Use annotations @NotNull and @Nullable Fail fast and use asserts to avoid propagation of null objects through the whole application when they shouldn't be null. Use equals with a known object first: if("knownObject".equals(unknownObject) Prefer valueOf() over toString(). Use null safe StringUtils methods StringUtils.isEmpty(null).
×
×
  • Create New...