0

what I am trying to do is post one record using postman using spring data JPA and I am getting null pointer Exception. 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

public interface EmployeeRepository  extends JpaRepository<Employee,Integer>{


}

-->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]

1 Answer 1

2

You have to use dependency injection. Here your private EmployeeRepository erepo is null which results in 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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.