简体   繁体   English

Spring + Thymeleaf绑定形式NullpointerException

[英]Spring + Thymeleaf binding form NullpointerException

I'm currently working on an user edit form. 我目前正在处理用户编辑表单。

I have a user object with the following structure (pseudo code): 我有一个具有以下结构(伪代码)的用户对象:

User

String name,
int age,
Address adress,
Email email,
User child,

getter/setter

Now, this object i put in as object in a Model like the following: 现在,我将该对象作为对象放入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;
}

in my template (i use thymeleaf) i bind these object like this: 在我的模板(我使用百里香)中,我像这样绑定这些对象:

<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>

In the GET-Method the template/html shows the correct data i expected. 在GET方法中,template / html显示我期望的正确数据。 Now i edit the fields and submit the form, then i get the following error: 现在,我编辑字段并提交表单,然后出现以下错误:

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]

All fields are transmitted correctly unless the email field of the child-object. 除非子对象的电子邮件字段,否则所有字段均正确传输。

I have no idea why the error occurs, the structure is the same, the data is valid and the funny thing is when I grab the child email element in a separate object goes it 我不知道为什么会发生错误,结构相同,数据有效并且有趣的是,当我在单独的对象中获取子电子邮件元素时

Does anyone have an idea? 有人有主意吗?

Thanks in advance. 提前致谢。

EDIT: Email class: 编辑:电子邮件类:

@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();
    }
}

The post method: 帖子方法:

@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);
}

The NullPointerException is happening in your Email.hashCode() method. NullPointerException正在您的Email.hashCode()方法中发生。 Looking at your code this could happen if value is null. 查看您的代码,如果value为null,则可能会发生这种情况。

The template you posted doesn't include the user.email.value field, so this is most likely why it is null. 您发布的模板不包含user.email.value字段,因此这很可能是null的原因。

Either you need to add a field for the user email, or change the hashCode method so that it handles the null value. 您需要为用户电子邮件添加一个字段,或者更改hashCode方法以使其处理空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM