You probably think I created an object with new so this is why I get this error but the answer is no...
Here is the simplest example I could make: https://github.com/victorqedu/SOQ001
So the error is:
java.lang.NullPointerException: Cannot invoke "com.example.demo.Subject.getText()" because "this.subject" is null
at com.example.demo.SearchBase.runTest(SearchBase.java:16) ~[classes/:na]
And the "subject" member is @Autowired and the class that contains it is not created with new, there is no "new" in all my project.
I think the origin of the problem is in the rest controller at the line "@RequestBody SearchTest object)" because somehow I think here is created a SearchTest instance automatically outside the Spring context. But this doesn't helps me to find a solution... I need to have a @RequestBody that converts to an object.
@RestController
@RequestMapping("/api")
public class TestRestController {
private final TestService service;
@Autowired
public TestRestController(TestService theService) {service = theService;}
@PostMapping("/test")
public String test(@RequestBody SearchTest object) {
return service.test(object);
}
}
To test I accessed from POSTMAN http://localhost:8080/api/test
LATER EDIT: As input to the POST method I provided this string: {"page":1}
I wrongly assumed that I can have extra attributes that are not in the input JSON and that I can @Autowire them... is not clear to me why this is not possible but obviously, this is the problem.
LATER EDIT: The basic question was why can't I autowire in this context.