2017-07-26 111 views
0

私は以下のエンドポイントを持つSpring-Boot APIを持っています。それは、SpringデータJPA findAllクエリでNull Pointer Exceptionをスローしています。私がこの行をコメントアウトすると、エラーは発生しません。リポジトリクエリからnullの結果が出ているようですが、データを直接クエリすることによってデータが存在することがわかります。私はtopicsLookup変数のnullを取得している理由を理解できません...誰かが正しい方向に私を指すことができますか?SpringデータJPAリポジトリfindAll()Nullポインタ

リソース:

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

} 

リポジトリ:

import org.acm.dl.api.domain.SpeakerTopic; 
import org.springframework.data.jpa.repository.JpaRepository; 

public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> { 

} 
+0

さらに詳しい情報があれば教えてください。私はそれを提供しようとします。 – littlewolf

+2

stacktraceが役立つ可能性があります。 speakerTopicsRepository自体がnullでないことを確認できますか?あなたはそれを購入しましたか? – NickJ

+0

@NickJそれだった。私は '@ Inject'アノテーションを忘れました。私は答えとして自由に投稿して、私はそれをマークします。 – littlewolf

答えて

1

最も可能性が高いCA speakerTopicsRepository自体がヌルであることを使用しています。これはおそらく、それをオートワイヤードすることを忘れることによって引き起こされます。

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

} 
関連する問題