java 当我更新我的资源时出现异常[duplicate]

tv6aics1  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(117)
    • 此问题在此处已有答案**:

What is a NullPointerException, and how do I fix it?(12个答案)
12小时前关门了。
我在SpringBoot中有一个API,当我更新我的资源时,SpringBoot抛出了NullPointerException,我不知道为什么。
SpringBoot告诉我:出现错误(类型=内部服务器错误,状态= 500)。无法调用"String. equalsIgnoreCase(String)",因为"com. packages. appRest. survey. Question. getId()"的返回值为空。无法调用"String. equalsIgnoreCase(String)",因为"com. packages. appRest. survey. Question. getId()"的返回值为空
服务类中的方法更新:

public void updateSurveyQuestion(String surveyId, String questionId, Question question) {
        List<Question> questions = retrieveAllSurveyQuestions(surveyId);
        questions.removeIf(q -> q.getId().equalsIgnoreCase(questionId));
        questions.add(question);
    }

控制器方法:

@RestController
public class SurveyResource {
    

    private SurveyService surveyService;

    public SurveyResource(SurveyService surveyService) {
        super();
        this.surveyService = surveyService;
    }

    @RequestMapping("/surveys/{surveyId}")
    public Survey retrieveSurveyById(@PathVariable String surveyId){
        Survey survey = surveyService.retrieveSurveyById(surveyId);

        if(survey == null){
            throw new ResponseStatusException(HttpStatus.NOT_FOUND);
        }

        return survey;
    }

    ////////
    @RequestMapping("/surveys/{surveyId}/questions")
    public List<Question> retrieveAllSurveyQuestions(@PathVariable String surveyId){
        List<Question> questions = surveyService.retrieveAllSurveyQuestions(surveyId);

        if(questions == null){
            throw new ResponseStatusException(HttpStatus.NOT_FOUND);
        }

        return questions;
    }

    ///////
    @RequestMapping("/surveys/{surveyId}/questions/{questionId}")
    public Question retrieveSpecificSurveyQuestions(@PathVariable String surveyId, @PathVariable String questionId){
        Question question = surveyService.retrieveSpecificSurveyQuestions(surveyId, questionId);

        if(question == null){
            throw new ResponseStatusException(HttpStatus.NOT_FOUND);
        }

        return question;
    }
    
    ////////
    @RequestMapping(value="/surveys/{surveyId}/questions", method=RequestMethod.POST)
    public ResponseEntity<Object> addNewSurveyQuestion(@PathVariable String surveyId, @RequestBody Question question){

        String questionId = surveyService.addNewSurveyQuestion(surveyId, question);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{questionId}").buildAndExpand(questionId).toUri();

        return ResponseEntity.created(location).build();
    }
    ////////

    ////////
    @RequestMapping(value="/surveys/{surveyId}/questions/{questionId}", method=RequestMethod.DELETE )
    public ResponseEntity<Object> deleteSurveyQuestion(@PathVariable String surveyId, @PathVariable String questionId){

        surveyService.deleteSurveyQuestion(surveyId, questionId);

        return ResponseEntity.noContent().build();
    }
    ////////

    @RequestMapping(value="/surveys/{surveyId}/questions/{questionId}", method = RequestMethod.PUT)
    public ResponseEntity<Object> updateSurveyQuestion(@PathVariable String surveyId, @PathVariable String questionId, @RequestBody Question question){

        surveyService.updateSurveyQuestion(surveyId, questionId, question);
        
        return ResponseEntity.noContent().build();
    }


}

实体类:

public class Question {

    public Question(){

    }

    public Question(String id, String description, List<String> options, String correctAnswer) {
        super();
        this.id = id;
        this.description = description;
        this.options = options;
        this.correctAnswer = correctAnswer;
    }

    ///////////////////

    private String id;

    private String description;

    private List<String> options;

    private String correctAnswer;

    //////////////////

    public String getId() {
        return this.id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getDescription() {
        return this.description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public List<String> getOptions() {
        return this.options;
    }
    public void setOptions(List<String> options) {
        this.options = options;
    }

    public String getCorrectAnswer() {
        return this.correctAnswer;
    }
    public void setCorrectAnswer(String correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    ///////////
    @Override
    public String toString() {
        return "{" +
            " id='" + getId() + "'" +
            ", description='" + getDescription() + "'" +
            ", options='" + getOptions() + "'" +
            ", correctAnswer='" + getCorrectAnswer() + "'" +
            "}";
    } 
}

我看不到错误

xzlaal3s

xzlaal3s1#

控制器接收到的Question对象可能有null id或解析问题,您可以通过记录对象或使用调试器来识别问题的来源或问题来进行检查。

@RequestMapping(value="/surveys/{surveyId}/questions/{questionId}", method = RequestMethod.PUT)
public ResponseEntity<Object> updateSurveyQuestion(@PathVariable String surveyId, @PathVariable String questionId, @RequestBody Question question){
    surveyService.updateSurveyQuestion(surveyId, questionId, question);
    LoggerFactory.getLogger(YourControllerImpl.class).info("Question receidved: {}", question);
    return ResponseEntity.noContent().build();
}

相关问题