Java NullPointerException

Java NullPointerException: The One Tiny Thing That’s Killing Your Chances of Solving

1.分解复杂的代码行

把多个对象放在不同行,在出现NullPointerException时,log就会准确的指出在哪一行哪一个对象为NULL
from

if ((user.isCustomer() != false) && (account.equals(id))) {
  ...
}

to

if ((user.isCustomer() != false) &&
    (account.equals(id))) {
  ...
}

2.空值检查

3.更冗长的日志记录

4.Developer tools to the rescue

5.Optional.ofNullable() on java 8

猜你喜欢

转载自blog.csdn.net/canyanruxue/article/details/81116277