-1

I am using MVC design pattern for my project, I have an interface called SchoolRepository and I have a controller class where I have declared the school repository interface, I have declared the school repository interface in model class as well, when I invoke the model class object where I have written the logic from the controller I am getting null pointer exception as the school interface declared in service class is null

below is my controller class

    @RestController 
    @RequestMapping("University")
    public class Controller{

        //interface declaration
        @Autowired
        SchoolRepository  schoolRepository;

        @GetMapping("student")
        public list getStudent(){

           Service s=new Service();
           List list=s.getStudents();
           return list;
        }
        .
        .
    }

below is my model class

      public class Service(){

      @Autowired
      SchoolRepository schoolRepository;

      public List getStudents(){
      return schoolRepository.getAll();
      }

below is the repository class

           @Repository
           public interface SchoolRepository extends JpaRepository<School, Long>{}

I get a null point exception in the Service class where I use schoolRepository, when I check the value of s.schoolRepository using System.Out.Println I get null value.. how to initialize the interface and how to use it in service class

varun
  • 25
  • 2
  • 11

1 Answers1

0

When you instantiate the service class in getStudent() method in your Controller class using "new", spring container doesn't come into picture. As the service object is not created by spring container, "schoolRepository" won't get autowired in your "Service" class and hence it will be null. So mark the Service class as @Service to make the spring container take care of the instantiation. Then instead of creating Service object using "new", autowire Service object in your Controller class. Make sure your repository class is marked as @Repository too.

Asha
  • 51
  • 6
  • I used the annotations and instead of creating the new service class I @Autowired the service class and I still get the same exception – varun Jul 07 '18 at 16:54
  • Did you annotate your repository class with @Repository? Can you paste code of your repository and service class? – Asha Jul 08 '18 at 04:30
  • Looks fine. Can you paste your updated Controller class? You have added @Service on your Service class, right? – Asha Jul 08 '18 at 10:36
  • the controller class is mentioned in the question, the remaining part of the controller has the mapping similar to the one shown above. I have added the annotation @Service to the service class – varun Jul 08 '18 at 16:51
  • Service s=new Service() -> This should be removed from getStudent() in controller. Is it done? I can still see the creation of new service object in the controller above. If you instantiate using "new", service bean instantiated by Spring won't come. To get the service bean created by Spring container, you have to autowire it. So, autowire service object in your controller like this -> `public class Controller{ @Autowired Service s; @GetMapping("student”) public list getStudent(){ List list=s.getStudents(); return list;` – Asha Jul 09 '18 at 04:25
  • Its working!! thanks Asha – varun Jul 11 '18 at 13:40
  • Great!! You can accept the answer. Thanks. – Asha Jul 11 '18 at 14:46