我目前正在处理用户编辑表单。
我有一个具有以下结构的用户对象(伪代码):
User
String name,
int age,
Address adress,
Email email,
User child,
getter/setter
现在,我把这个对象作为对象放在Model
中,如下所示:
@RequestMapping(value = "/{login}/edit", method = RequestMethod.GET)
public ModelAndView editUserByLogin(@PathVariable("login") final String login) {
final User currentUser = UserRepository.findPersonByLogin(login);
ModelAndView mav = new ModelAndView(URL_EDIT_USER);
mav.addObject(MODEL_USER, currentUser);
return mav;
}
在我的模板中(我使用百里香)我像这样绑定这些对象:
<form class="form-horizontal" method="post" >
<div class="form-group">
<label class="col-sm-2 control-label" th:text="#{user.name}">name</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{user.name}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" th:text="#{user.address.street}">name</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{user.address.street}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" th:text="#{user.child.email.value}">name</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{user.child.email.value}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" th:text="#{user.child.address.street}">name</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{user.child.address.street}" />
</div>
</div>
</form>
在GET-Method中,template / html显示了我预期的正确数据。现在我编辑字段并提交表单,然后我收到以下错误:
java.lang.NullPointerException: null
at com.example.user.model.Email.hashCode(EMail.java:66) ~[classes/:na]
at java.lang.Object.toString(Unknown Source) ~[na:1.8.0_25]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_25]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_25]
at org.springframework.validation.FieldError.toString(FieldError.java:102) ~[spring-context-4.0.8.RELEASE.jar:4.0.8.RELEASE]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_25]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_25]
at org.springframework.validation.AbstractErrors.toString(AbstractErrors.java:243) ~[spring-context-4.0.8.RELEASE.jar:4.0.8.RELEASE]
at org.springframework.validation.BindException.getMessage(BindException.java:284) ~[spring-context-4.0.8.RELEASE.jar:4.0.8.RELEASE]
at java.lang.Throwable.getLocalizedMessage(Unknown Source) ~[na:1.8.0_25]
at java.lang.Throwable.toString(Unknown Source) ~[na:1.8.0_25]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_25]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_25]
除非子对象的电子邮件字段,否则将正确传输所有字段。
我不知道为什么会发生错误,结构是一样的,数据是有效的,而有趣的是当我在一个单独的对象中抓取子电子邮件元素时它是
有没有人有想法?
提前致谢。
修改 电子邮件课程:
@Entity(name="Email")
public class Email implements Serializable
{
private static final long serialVersionUID = 6891079722082340011L;
@Id()
@GeneratedValue(strategy=GenerationType.AUTO)
@Expose
protected Long eMailId;
@Expose
protected String value;
//getter/setter
@Override
public boolean equals(Object obj)
{
if(obj instanceof EMail){
return value.equals(((Email) obj).getValue());
}
return false;
}
@Override
public int hashCode()
{
return value.hashCode();
}
}
帖子方法:
@RequestMapping(value = "/{login}/edit", method = RequestMethod.POST)
public ModelAndView updateUser(@PathVariable("login") final String login, @ModelAttribute final User webUser) {
final Person repoUser = personRepository.findPersonByLogin(login);
repoUser.updateWith(webUser);
return new ModelAndView("redirect:" + URL_USERS_OVERVIEW);
}
答案 0 :(得分:1)
NullPointerException正在Email.hashCode()
方法中发生。如果value
为空,可能会发生这种情况。
您发布的模板不包含user.email.value
字段,因此很可能为空。
您需要为用户电子邮件添加字段,或更改hashCode
方法以便它处理空值。