spring Sping Boot 时Java.lang.空指针异常:零

zzwlnbp8  于 2023-02-18  发布在  Spring
关注(0)|答案(5)|浏览(190)

我正在尝试使用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);
    }    
}

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

....

gwbalxhn

gwbalxhn1#

在分析给出的代码片段时,由于您的代码没有要求Spring依赖项注入器将EmployerService作为EmployerController的依赖项注入,因此发生了空指针异常。因此它不会将EmployerService bean类注入到引用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);
    }    
}
vpfxa7rd

vpfxa7rd2#

private EmployerService employerService;

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

hfyxw5xn

hfyxw5xn3#

使用前需要获取仓库的对象,为此添加@Autowired on

private Repository repository;

在你的雇主服务课上。

kpbpu008

kpbpu0084#

@Autowired绝对是解决方案。
但是你的服务类,如果你要求你的REPOSITORY,你也应该放在那里。
所以在我的问题中,解决方案是

@Autowired
private ProductService productService;

还有

@Autowired
ProductRepository productRepository;
bf1o4zei

bf1o4zei5#

在抛出NullpointerException的方法中有一行代码,然后我将该行代码放入if语句中,它就无缝地为我工作了。例如:
if (user != null) { application.setEmailaddress(user.getEmail());}

相关问题