Witam, mam problem z odczytaniem danych z bazy danych po wywołaniu metody getOne(). Zaznaczam, że jestem początkującym i mam wrażenie, że jest to głupi błąd nad którym już trochę siedzę, więc piszę z prośbą o pomoc. Więc mam to zrobione w taki sposób, że po przejściu z odnośnika do następnej strony zapisuje się w URLu id przedmiotu, które następnie pobieram w kontrolerze i na jego podstawie zapisuję sobie cały obiekt w modelu. Jednak wywołanie metody
model.addAttribute("currentSubject", subjectService.findSubjectByID(Long.parseLong(idSubject, 10)));`
wyrzuca NullPointerExcepion nawet wtedy gdy na sztywno wpiszę subjectService.findSubjectByID(2) i wiem że w bazie z id 2 znajduje się obiekt.
Metoda kontrolera która wyrzuca wyjątek:
@Controller
public class LessonController {
private SubjectService subjectService;
//private LessonService lessonService;
@GetMapping("/user/allLessons")
public String showLessonsInSubject(@RequestParam(defaultValue="id") String idSubject, Model model) {
model.addAttribute("idSubject", idSubject);
model.addAttribute("currentSubject",
subjectService.findSubjectByID(Long.parseLong(idSubject, 10)));
return "user/allLessons";
}
}
@Entity
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_subject")
private Long idSubject;
@NotEmpty
private String name;
@NotEmpty
@Column(length = 1000)
@Type(type="text")
private String description;
@OneToMany(mappedBy = "subject", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Lesson> lessons;
public Subject() {}
public Subject(Long id, String name, String descrpition) {
super();
this.idSubject = id;
this.name = name;
this.description = description;
}
public Long getId() {
return idSubject;
}
public void setId(Long id) {
this.idSubject = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Lesson> getLessons() {
return lessons;
}
public void setLessons(List<Lesson> lessons) {
this.lessons = lessons;
}
@Override
public String toString() {
return "Subject [id=" + idSubject + ", name=" + name + ", description=" + description + ", lessons=" + lessons + "]";
}
}
@Service
public class SubjectService {
private SubjectRepository subjectRepository;
@Autowired
public void setSubjectRepository(SubjectRepository subjectRepository) {
this.subjectRepository = subjectRepository;
}
public Subject findSubjectByID(Long idSubject) {
return subjectRepository.getOne(idSubject);
}
}
Z góry dziękuje za wszelką pomoc.