在过去的几个小时里,我一直在撞墙。我正在尝试为网站写一个post方法,它一直给我除错误以外的任何东西,最新的是NullPointer异常。以下是相关的模型(不包括getter和setter:
翻译:
@Entity
@Table(name = "translations")
public class Translation {
@Id
@GeneratedValue
private long id;
@OneToOne
private User user;
@Column(length = 9999)
private String userTranslation;
@Column(columnDefinition = "text")
private String description_of_changes;
@Column (nullable = false)
private boolean flag_problem;
@Column(columnDefinition = "text")
private String reason;
@OneToOne
private Request request;
@OneToOne
private Translation_Status status;
请求:
@Entity
@Table(name = "requests")
public class Request {
@Id
@GeneratedValue
private long id;
@Column(unique = true, length = 1000)
private String web_page;
@Column(columnDefinition = "text")
private String untranslated_text;
@Column(columnDefinition = "text")
private String description;
@OneToOne
private Department department;
@OneToOne
private User user;
@Column(columnDefinition = "text")
private String google_translate;
@OneToOne
private Request_Status status;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date time;
这是模板和表单的html:
<div class="form-group">
<form id="translateForm" th:action="@{/translate}" th:method="post" th:object="${newTranslation}">
<label for="untranslatedText">Untranslated Text</label>
<br/>
<textarea id="untranslatedText" name="untranslatedText" rows="10" th:text="${translationRequest.getUntranslated_text()}" readonly="readonly"></textarea>
<br/>
<label for="translatedText">Google Translated Text; Please make edits here</label>
<br/>
<textarea name="translatedText" id="translatedText" rows="10" th:text="${translationRequest.getGoogle_translate()}" th:field="*{userTranslation}"></textarea>
<label for="description">Please describe any changes you made</label>
<textarea name="description" id="description" rows="10" th:field="*{description_of_changes}"></textarea>
<label for="flagProblem">Are there any severe problems with the Google Translations?</label>
<select name="flagProblem" id="flagProblem" th:field="*{flag_problem}">
<option value="true">Yes</option>
<option value="false">No</option>
</select>
<br/>
<!--TODO Look into a hidden input that becomes unhidden if above input is true-->
<label for="reason">If you answered yes to the above question please explain why</label>
<input type="text" id="reason" name="reason" th:field="*{reason}"/>
<input type="hidden" id="refRequest" name="refRequest" th:value="${translationRequest.id}" th:field="*{request}"/>
<input type="submit" class="btn btn-block btn-primary" value="Submit Translation"/>
</form>
</div>
这是post方法:
@PostMapping("/translate")
public String submitTranslation(@ModelAttribute Translation newTranslation){
User testUser = userDao.findOne(1L);
newTranslation.setUser(testUser);
Translation_Status translationStatus = translationStatusDao.findOne(101L);
newTranslation.setStatus(translationStatus);
long time = date.getTime();
Timestamp ts = new Timestamp(time);
newTranslation.setTime(ts);
Long id = newTranslation.getRequest().getId();
System.out.println(id);
Request change = requestDao.findOne(id);
Request_Status newStatus = requestStatusDao.findOne(201L);
change.setStatus(newStatus);
Translation savedTranslation = translationDao.save(newTranslation);
Request changedRequest = requestDao.save(change);
return "index";
}
我的IDE告诉我尝试获取newTranslation.getRequest()。getId();时发生错误。但是我不知道为什么我可以毫无问题地调用翻译对象的任何其他部分。有人可以帮我弄清楚我在做什么错吗?