Benzer sorunlar için SO'ya baktım ve bazı ilgili soruları yanıtlamamış olmasına rağmen hiçbiri yanıtlanmadı. Bu yüzden sorunumu daha iyi şanslar için umuyorum.Thymeleaf ve Spring Boot - NullPointerException'ı kullanarak çocuklarla kaydı güncelleyemiyor - Getting NullPointerException
Çocuklarla (Bire Çok) ResourceSkills
numaralı bir üst kayıt Resource
var. BenNull olduğunu denetlemek benim denetleyici sınıfında görüyorum, ancak ben kaynak düzenleme için kullandığım Thymeleaf sayfamda hem Resource
ve ResourceSkills
değerleri görüntüleyebilirsiniz. Bu bir NullPointerException ile sonuçlanır.
Kaynak
@Entity
public class Resource {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Version
private Integer version;
private String firstName;
private String lastName;
@OneToMany (mappedBy = "resource", orphanRemoval=false, fetch=FetchType.EAGER)
private List<ResourcesSkills> skills;
@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name="project_id")
private Project project;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<ResourcesSkills> getSkills() {
return skills;
}
public void setSkills(List<ResourcesSkills> skills) {
this.skills = skills;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
ResourcesSkills
@Entity
@IdClass(ResourcesSkillsId.class)
public class ResourcesSkills {
@Id
@ManyToOne
@JoinColumn(name="resource_id")
private Resource resource;
@Id
@ManyToOne
@JoinColumn(name = "skill_id")
private Skill skill;
@Version
private Integer version;
private boolean primarySkill;
public Resource getResource() {
return resource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public boolean isPrimarySkill() {
return primarySkill;
}
public void setPrimarySkill(boolean primarySkill) {
this.primarySkill = primarySkill;
}
}
ResourceController
: Yani buradakodudur
@Controller
public class ResourceController {
ResourceService resourceService;
SkillService skillService;
@Autowired
public void setResourceService(ResourceService resourceService){
this.resourceService = resourceService;
}
@Autowired
public void setSkillService(SkillService skillService){
this.skillService = skillService;
}
@RequestMapping(value="resources", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("resources", resourceService.listAllResources());
return "resources";
}
@RequestMapping(value="resource/{id}")
public String showResource(@PathVariable Integer id, Model model){
model.addAttribute("resource", resourceService.getResourceById(id));
return "resourceshow";
}
@RequestMapping(value = "resource/new")
public String newResource(Model model){
model.addAttribute("resource", new Resource());
model.addAttribute("skills", skillService.listAllSkills());
return "resourceform";
}
@RequestMapping(value = "resource/edit/{id}")
public String editResource(@PathVariable Integer id, Model model){
Resource resource = resourceService.getResourceById(id);
model.addAttribute("resource", resource);
return "resourceform";
}
@RequestMapping(value = "resource/delete/{id}")
public String deleteResource(@PathVariable Integer id){
resourceService.deleteResource(id);
return "redirect:/resources";
}
@RequestMapping(value = "resource", method = RequestMethod.POST)
public String saveResource(Resource resource){
/* AT THIS POINT resource.skills is NULL and save FAILS */
ResourceService.saveResource(resource);
return "redirect:/resource/" + resource.getId();
}
}
Ve nihayet Thymeleaf kodu:
<h2>Resource Details</h2>
<div>
<form class="form-horizontal" th:object="${resource}" th:action="@{/resource}" method="post">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{version}"/>
<div class="form-group">
<label class="col-sm-2 control-label">First name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{firstName}"/>
</div>
<label class="col-sm-2 control-label">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{lastName}"/>
</div>
</div>
<div>
<table class="table table-stripped">
<tr>
<th>Skill</th>
<th>Primary</th>
</tr>
<tr th:each="resSkill : *{skills}">
<td th:text="${resSkill.skill.description}">Skill Description</td>
<td><input type="checkbox" name="primary" th:disabled="disabled" th:checked="${resSkill.primarySkill}" /></td>
</tr>
</table>
<a th:href="${'/resourceSkills/' + resource.id}">Edit Skills</a>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
alıyorum hatası (referans için) şudur: Eğer ResourceService ve @Autowire unuttum gibi
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at eu.kororos.ccplanner.controllers.ResourceController.saveResource(ResourceController.java:79) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
Marco'nun yanıtı için teşekkür ederim ama aldığımdan emin değilim ... Bu, ResourceController'ın ilk iki yönteminde yapıyorum. Üstüne bir şey yapmam gerekiyor mu? – Lefteris