获取请求时出现500内部服务器错误,NullPointerException

问题描述 投票:1回答:1

我正在做一个春季启动项目,

我试图从邮递员发送获取请求,但我得到500内部服务器错误。

请帮我改进代码。

控制器:

@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:4200")
public class sessionMcqController {

@Autowired
private sessionMcqService service;

@GetMapping("/viewmcq")
public List<Question> getAll(){
    return service.getAll();
}

}

楷模:

1.问题模型:

@Entity
@Table(name = "mcq_question")
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JoinColumn (name = "questionId")
private Long questionId;

@Column(nullable = false)
private String question;

@OneToOne(fetch = FetchType.LAZY, targetEntity = Option.class)
@JoinTable( name = "mcq_answer", joinColumns = @JoinColumn ( name = "question_id"), inverseJoinColumns = @JoinColumn( name = "option_id"))
private List <Option> options;

@OneToMany(mappedBy = "questionId", cascade = CascadeType.ALL)
private Set<Option> option;

public Question() { }

public Long getMcqId() {
    return questionId;
}

public void setMcqId(Long mcqId) {
    this.questionId = mcqId;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

@Override
public String toString() {
    return "McqModel{" +
            "mcqId=" + questionId +
            ", question='" + question + '\'' +
            '}';
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Question question1 = (Question) o;
    return questionId.equals(question1.questionId) &&
            question.equals(question1.question) &&
            options.equals(question1.options) &&
            option.equals(question1.option);
}

@Override
public int hashCode() {
    return Objects.hash(questionId, question, options, option);
}
}

2. OptionModel:

@Entity
@Table(name = "mcq_Options")
public class Option {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "option_id")
private int optionId;

private String option;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn
private Question questionId;

public Option() { }

public Option(String option, Question questionId) {
    this.option = option;
}

public int getOptionId() {
    return optionId;
}

public void setOptionId(int optionId) {
    this.optionId = optionId;
}

public String getOption() {
    return option;
}

public void setOption(String option) {
    this.option = option;
}

public Question getQuestionId() {
    return questionId;
}

public void setQuestionId(Question questionId) {
    this.questionId = questionId;
}

@Override
public String toString() {
    return "Option{" +
            "optionId=" + optionId +
            ", option='" + option + '\'' +
            ", questionId=" + questionId +
            '}';
}
}

存储库:1.QuestionRepository

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
}

2.OptionRepository

@Repository
public interface OptionRepository extends JpaRepository<Option, Integer> {
}

服务:

@Service
public class sessionMcqService {

@Autowired
private QuestionRepository repository;

public List<Question> getAll(){

    return repository.findAll();
}

}

错误:

java.lang.NullPointerException:null at com.bluepi.TrainingProject.controller.sessionMcqController.getAll(sessionMcqController.java:22)〜[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8 .0_201] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_201] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_201] at at java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_201] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)〜[spring-web -5.1.5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)〜[spring-web-5.1.5.RELEASE.jar: 5.1.5.RELEASE]在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.REL EASE]在组织的org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]。 org.springframework.web.servlet上的springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] .mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet。 java:1038)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)〜[spring-webmvc-5.1 .5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5。发布在org.springfra mework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE],位于javax.servlet.http.HttpServlet.service(HttpServlet.java) :634)〜[tomcat-embed-core-9.0.16.jar:9.0.16]在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)〜[spring-webmvc-5.1.5。在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)〜[tomcat-embed-core-9.0.16.jar:9.0.16]的RELEASE.jar:5.1.5.RELEASE]

java spring-boot servlets
1个回答
1
投票

您需要在@Autowired上的private sessionMcqService service;线上方添加sessionMcqController.java

© www.soinside.com 2019 - 2024. All rights reserved.