Spring 开机 java.lang.NullPointerException: 空

Spring boot java.lang.NullPointerException: null

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

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

我尝试更改该值,但没有成功

EmployerService.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);
    }    
}

EmployerController.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 依赖注入器将 EmployerService 作为对 EmployerController 的依赖注入,因此它不会将 EmployerService bean class 注入到引用 private EmployerService employerService; 中,因此它在 EmployerController 中为空。您可以通过在 EmployerController

中添加 @Autowire 注释 private EmployerService service; 引用来要求 Spring 依赖注入器注入依赖

将您的 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);
    }    
}

你需要在使用之前获取存储库的对象,为此添加@Autowired

private Repository repository;

在您的雇主服务中 class。

 private EmployerService employerService;

这意味着您创建的是 EmployerService 的引用变量,而不是 EmployerService 的对象。这可以通过使用 new 关键字来完成。但是如您所知 Spring Container 使用 DI(依赖注入)来管理 bean(一个对象,在上面的例子中是 EmployerService 的对象)。所以一个对象的对象实例化和整个生命周期都是由spring管理的。为此,我们必须告诉这个对象应该由 spring 管理,这是通过使用 @Autowired 注释完成的。

@Autowired 绝对是解决方案。

但是你的服务class,如果你要求你的REPOSITORY,你也应该放在那里。

所以在我的问题中,解决方案是

@Autowired
private ProductService productService;

@Autowired
ProductRepository productRepository;