gpt4 book ai didi

html - Spring 启动 java.lang.NullPointerException : null

转载 作者:行者123 更新时间:2023-12-02 00:14:59 25 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 和 REST -API 构建 Spring-boot CRUD 应用程序。但是,当我尝试运行该应用程序时,一切正常,但控制台显示以下错误

java.lang.NullPointerException: null
at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na]

我试图改变这个值,但是没有用

雇主服务.java

package io.javabrains;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import io.javabrains.Entity.Employer;

@Service
public class EmployerService {

private Repository repository;

public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;

}

public void addEmployer(Employer employer) {
repository.save(employer);
}
}

雇主 Controller .java

package io.javabrains;

import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.javabrains.Entity.Employer;

@RestController
public class EmployerController {

private EmployerService service;


@RequestMapping("/employer")
public List<Employer>getAllEmployers()
{
return service.getAllEmployers();
}
/*
* @RequestMapping("/employer/{id}") public Employer getEmployer(@PathVariable
* int id) { return service.getEmployer(id); }
*/

@RequestMapping(method=RequestMethod.POST,value="/employer/create")
public void addEmployer(@RequestBody Employer employer) {
service.addEmployer(employer);
}
}

....

最佳答案

根据给定的代码片段的分析,空指针异常发生是因为您的代码没有要求 spring 依赖注入(inject)器将 EmployerService 作为对 EmployerController 的依赖注入(inject),因此它不会将 EmployerService bean 类注入(inject)引用 private EmployerService employerService; 因此它在 EmployerController 中为空。您可以通过在 EmployerController

中添加 @Autowire 注解 private EmployerService service; refence 来要求 Spring Dependency Injector 注入(inject)依赖

将您的 EmployerService 更新为以下内容即可

package io.javabrains;

import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.javabrains.Entity.Employer;

@RestController
public class EmployerController {

//UPDATE : Autowiring
@Autowired
private EmployerService employerService;


@RequestMapping("/employer")
public List < Employer > getAllEmployers() {
return service.getAllEmployers();
}
/*
* @RequestMapping("/employer/{id}") public Employer getEmployer(@PathVariable
* int id) { return employerService.getEmployer(id); }
*/

@RequestMapping(method = RequestMethod.POST, value = "/employer/create")
public void addEmployer(@RequestBody Employer employer) {
employerService.addEmployer(employer);
}
}

此外,当尝试访问 repository 时,Service 中也会出现同样的问题。

更新EmployeeService.java代码包含@autorwired逻辑:

package io.javabrains;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import io.javabrains.Entity.Employer;

@Service
public class EmployerService {

@Autowired
private Repository repository;

public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;

}

public void addEmployer(Employer employer) {
repository.save(employer);
}
}

关于html - Spring 启动 java.lang.NullPointerException : null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com