Error NullPointerException in Java (Spring-boot)

3

I already know that this is a very common error and it happens when trying to access a NULL type object, the problem is that I do not know why it is NULL if I'm supposed to be instantiating it, I have a class called NoteController in the that instantiates an object of another class called CountryController, this CountryController class has a simple method that returns True or False when it makes a query to the database, I need to call this method from the NoteController class but it always throws the exception NullPointerException, ¿Any do you know why? ... in advance Thank you very much ..

// This is my NoteController class

//CLASE  NoteController
   package com.myapp.controller;

   import com.myapp.model.Notes;
   import com.myapp.repository.NoteRepository;
   import com.myapp.controller.CountryController;

   @RestController
   @RequestMapping("/api")
   public class NoteController {
     @Autowired
     NoteRepository noteRepository;
     CountryController objCountry = new CountryController();

     //Create a new Note Validation Country
     @PostMapping("/notes")
     public String createNote(@Valid @RequestBody Notes note) {      
         try {
             if (objCountry.existCountry((note.getNOT_COUNTRY()))) {
                 noteRepository.save(note);
                 return "Nota Guardada";
             }else {
                 return "Nota no Guardada";
             }
         }catch(NullPointerException e) {
             return "Se produjo un  error "+ e;
         }
     }
 }

// THIS IS MY CountryController CLASS

//Clase CountryController

package com.myapp.controller;
import com.MyApp.model.Country;
import com.MyApp.repository.CountryRepository;

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


    public CountryController() {}

    @Autowired
    CountryRepository countryRepository;

    //Verify Country
    public Boolean existCountry(String countryId) {
     if (countryRepository.findById(countryId).isPresent()) {
         return true;    
     }else {
         return false;
     } 
    }
}   
    
asked by Cristian D. 28.03.2018 в 21:35
source

3 answers

4

In NoteController you have:

@RestController
@RequestMapping("/api")
public class NoteController {
    @Autowired
    NoteRepository noteRepository;
    CountryController objCountry = new CountryController();
    ...
}

But CountryController is also a component of Spring:

@RestController
@RequestMapping("/api")
public class CountryController {
    @Autowired
    CountryRepository countryRepository;
    ...
}

By making new CountryController() your components that have @Autowired will not be initialized and will remain null , instead of creating it yourself, delegate the creation to Spring as you already do with your NoteRepository and CountryRepository :

@RestController
@RequestMapping("/api")
public class NoteController {
    @Autowired
    NoteRepository noteRepository;
    @Autowired
    CountryController objCountry;
    ...
}
    
answered by 28.03.2018 / 22:00
source
4

The dependence injection is handled by Spring. You should never create a Controller, Service or Repository with the reserved word "new", this task is delegated to the framework.

    
answered by 28.03.2018 в 21:49
4

No need to create with the reserved word new.

Spring uses the I principle of SOLID, which is the injection of dependencies.

to inject a class just put the @Autowired tag

in your case:

@Autowired
    CountryController objCountry;
    
answered by 28.03.2018 в 22:05