简体   繁体   English

使用数据 JPA 在 Spring Boot 中通过邮递员插入记录时出现空指针异常

[英]Null Pointer Exception on insert record through postman in Spring Boot using Data JPA

what I am trying to do is post one record using postman using spring data JPA and I am getting null pointer Exception.我想要做的是使用邮递员使用弹簧数据 JPA 发布一条记录,并且我收到空指针异常。 I don't know what is going wrong with no error in the request , no error in code but it is showing internal server error.我不知道有什么问题,请求中没有错误,代码中没有错误,但它显示内部服务器错误。 Please anyone, help me请任何人,帮助我

Controller-->控制器-->

@RestController
@RequestMapping("/api")
public class Controller1 {

    private  EmployeeRepository erepo;
    @GetMapping("/getEmployee")
    public ResponseEntity<Object> get(){
        List<Employee> findAll = erepo.findAll();
        return new ResponseEntity<Object>(findAll,HttpStatus.OK);

    }

    @PostMapping("/postEmployee")
    public String post(@RequestBody Employee emp){
        System.out.println(emp.getName());
        System.out.println(emp.getId());
        erepo.save(emp);

        return "string";
    }   
}

-->Entity -->实体

@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Employee(String name) {
        super();
        this.name = name;
    }
    public Employee(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

-->JpaRepoitory --> JpaRepoitory

public interface EmployeeRepository  extends JpaRepository<Employee,Integer>{


}

-->application.properties -->application.properties

server.port=8082


spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ajax1
spring.datasource.username=root
spring.datasource.password=rajat

spring.jpa.hibernate.ddl-auto = update

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

-->Postman request -->邮递员请求

Url-->http://localhost:8082/api/postEmployee
body-->{
    "id":1,

    "name":"kanha"
}

Error stack frame-->错误堆栈帧-->

java.lang.NullPointerException: null
    at com.example.demo.controller.Controller1.post(Controller1.java:31) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]

You have to use dependency injection.你必须使用依赖注入。 Here your private EmployeeRepository erepo is null which results in java.lang.NullPointerException: null这里你的private EmployeeRepository ereponull导致java.lang.NullPointerException: null

Change改变

private EmployeeRepository erepo;

To

@Autowired private EmployeeRepository erepo;

Note: You don't need to pass "id":1 as you are using @GeneratedValue(strategy=GenerationType.AUTO)注意:在使用@GeneratedValue(strategy=GenerationType.AUTO) ,您不需要传递"id":1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM