小编Ale*_*lex的帖子

Spring启动java.lang.NullPointerException:null

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

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

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

雇主服务.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);
    }    
}
Run Code Online (Sandbox Code Playgroud)

雇主控制器.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") …
Run Code Online (Sandbox Code Playgroud)

html spring web-development-server web spring-boot

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

html ×1

spring ×1

spring-boot ×1

web ×1

web-development-server ×1