Spring boot java.lang.NullPointerException: null

15,063

Solution 1

On the analysis of the code snippet given, the null pointer exception occurred since your code doesn't ask the spring dependency injector to inject EmployerService as a dependency to EmployerController, so it doesn't inject the EmployerService bean class to the reference private EmployerService employerService; thus it is null in EmployerController. You can ask Spring Dependency Injector to inject dependency by adding @Autowire annotation private EmployerService service; refence in EmployerController

Update your EmployerService to the following will work

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

And Also, the same issue would occur in Service when trying to access repository.

Update EmployeeService.java code include @autorwired logic:

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

Solution 2

 private EmployerService employerService;

This mean you have created a reference variable of EmployerService not an object of EmployerService. Which can be done by using a new keyword. But as you know Spring Container uses DI(Dependency Injection) to manage a beans(an object, in above case object of EmployerService). So the object instantiation and whole lifecycle of an object is managed by the spring. For this we have to tell that the this object should be managed by the spring which is done by using @Autowired annotation.

Share:
15,063
Alex
Author by

Alex

Updated on July 26, 2022

Comments

  • Alex
    Alex almost 2 years

    I am trying to build Spring-boot CRUD application with Hibernate and REST -API.However when I try to run the app everything is working fine but console is displaying the following error

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

    I tried to change the value however it didnt work

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

    ....