【问题标题】:Spring boot java.lang.NullPointerException: null春季启动 java.lang.NullPointerException: null
【发布时间】:2019-07-29 04:45:48
【问题描述】:

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

....

【问题讨论】:

    标签: html spring spring-boot web web-development-server


    【解决方案1】:

    分析给出的sn-p代码,出现空指针异常是因为你的代码没有要求spring依赖注入器将EmployerService作为EmployerController的依赖注入,所以它没有注入EmployerService bean 类到引用 private EmployerService employerService; 因此它在 EmployerController 中为空。您可以通过在EmployerController 中添加@Autowire 注释private EmployerService service; 引用来要求Spring Dependency Injector 注入依赖项

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

    【讨论】:

      【解决方案2】:
       private EmployerService employerService;
      

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

      【讨论】:

        【解决方案3】:

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

        private Repository repository;
        

        在您的 EmployerService 类中。

        【讨论】:

          【解决方案4】:

          @Autowired 绝对是解决方案。

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

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

          @Autowired
          private ProductService productService;
          

          @Autowired
          ProductRepository productRepository;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-11
            • 2015-04-18
            • 2017-06-24
            • 2015-08-22
            • 2015-09-18
            • 2015-03-20
            • 2018-01-24
            • 2016-08-22
            相关资源
            最近更新 更多