Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Spring boot java.lang.NullPointerException:null_Html_Spring_Spring Boot_Web_Web Development Server - Fatal编程技术网

Html Spring boot java.lang.NullPointerException:null

Html Spring boot java.lang.NullPointerException:null,html,spring,spring-boot,web,web-development-server,Html,Spring,Spring Boot,Web,Web Development Server,我正在尝试使用Hibernate和REST-API构建Spring bootCRUD应用程序。但是,当我尝试运行该应用程序时,一切正常,但控制台显示以下错误 java.lang.NullPointerException: null at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na] 我试图改变这个值,但它不起作用 EmployerService.ja

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

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);
    }    
}
包io.javabrains;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.stereotype.Service;
导入io.javabrains.Entity.Employer;
@服务
公共类雇员服务{
私有存储库;
公共列表GetAllEmployers(){
Listemployers=newarraylist();
repository.findAll()
.forEach(雇主::添加);
返回雇主;
}
公共无效雇主(雇主){
保存(雇主);
}    
}
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);  
    }
}
包io.javabrains;
导入java.util.List;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
导入io.javabrains.Entity.Employer;
@RestController
公共类EmployeerController{
私人雇员服务;
@请求映射(“/雇主”)
公共列表GetAllEmployers()
{
return service.getAllEmployers();
}
/*
*@RequestMapping(“/employer/{id}”)公共雇主getEmployer(@PathVariable
*int id){return service.getEmployer(id);}
*/
@RequestMapping(method=RequestMethod.POST,value=“/employer/create”)
公共无效addEmployer(@RequestBody-Employer){
服务。雇主(雇主);
}
}

..

在对给定代码段的分析中,出现空指针异常,因为您的代码没有要求spring依赖项注入器将
EmployerService
作为对
EmployerController
的依赖项注入,因此它不会将
EmployerService
bean类注入到引用
私有EmployerService EmployerService中因此在
EmployeerController
中为空。您可以通过添加
@Autowire
annotation
private employeerservice服务,请求Spring依赖注入器注入依赖员工控制器中的参考

将您的
employeerservice
更新到以下位置即可

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

在使用之前,您需要获取存储库的对象,以便添加@Autowired on

private Repository repository;
在你的员工服务课上

 private EmployerService employerService;

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

@Autowired无疑是解决方案

但是您的服务类,如果您要求您的存储库,您也应该放在那里

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

@Autowired
private ProductService productService;

@Autowired
private ProductService productService;
@Autowired
ProductRepository productRepository;