在寫實體類時,經(jīng)常會對域增加校驗,例如@NotNull表示哪個字段不能為空,昨天晚上調(diào)試代碼,就遇到了問題,
@Entitypublic class ApplicationCategory implements Serializable { private static final long serialVersionUID = -8018302345969463947L; @Id @GeneratedValue private Integer id; @NotNull(message = "應用分類名稱不能為空") private String name; private String remark; @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + name.hashCode(); result = 31 * result + (remark != null ? remark.hashCode() : 0); return result; } ..... ..... //省略其他方法}
程序啟動,保存applicationCategory時,拋異常,錯誤如下:
2017-05-23 18:51:43.195 ERROR 16876 --- [http-nio-65009-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.ValidationException: HV000041: Call to TraversableResolver.isReachable() threw an exception.] with root cause java.lang.NullPointerException: null at com.xxx.hhhh.cccc.machine.domain.ApplicationCategory.hashCode(ApplicationCategory.java:69