Reputation: 181
I have a Spring-Boot API with the endpoint below. It is throwing a Null Pointer Exception on a Spring Data JPA findAll
query; when I comment out this line, I get no errors. It seems that I am getting a null result from the repository query, but I know the data is there from querying the DB directly. I cannot understand why I'm getting a null for topicsLookup
variable... Can anyone point me in the right direction?
Resource:
@RequestMapping(value = "/lectures/{lectureId}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, SpeakerTopicLectures> getLecture(@PathVariable Long lectureId){
Long requestReceived = new Date().getTime();
Map<String, SpeakerTopicLectures> result = new HashMap<>();
log.debug("** GET Request to getLecture");
log.debug("Querying results");
List<SpeakerTopicLectures> dataRows = speakerTopicLecturesRepository.findBySpeakerTopicLecturesPk_LectureId(lectureId);
// This line throws the error
List<SpeakerTopic> topicsLookup = speakerTopicsRepository.findAll();
// Do stuff here...
log.debug("Got {} rows", dataRows.size());
log.debug("Request took {}ms **", (new Date().getTime() - requestReceived));
// wrap lecture in map object
result.put("content", dataRows.get(0));
return result;
}
Java Bean:
@Entity
@Table(name = "speaker_topics")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class SpeakerTopic implements Serializable {
@Id
@Column(name = "topic_id")
private Long topicId;
@Column(name = "topic_nm")
private String topicName;
@Column(name = "topic_desc")
private String topicDesc;
@Column(name = "topic_acm_relt_rsce")
private String relatedResources;
}
Repository:
import org.acm.dl.api.domain.SpeakerTopic;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> {
}
Upvotes: 3
Views: 14857
Reputation: 159
Try using
@Repository
@Transactional
public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> {
// Your Repository Code
}
I think @Repository
& @Transactional
is missing. Please use it.
Upvotes: 1
Reputation: 9559
The most likely cause is that speakerTopicsRepository
is itself null, which is probably caused by forgetting to autowire it, e.g.
public class YourController {
@Autowired private SpeakerTopicsRepository speakerTopicsRepository;
@RequestMapping(value = "/lectures/{lectureId}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, SpeakerTopicLectures> getLecture(@PathVariable Long lectureId) {
// your method...
}
}
Upvotes: 7