-4

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.

Victorqedu
  • 484
  • 4
  • 20
  • 2
    The exception you show does not come from the code you show. Please provide a [mre]. A likely duplicate of your question is [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – Mark Rotteveel Apr 12 '23 at 13:31
  • Is not a duplicate, I already mentioned in the question that I don't use new to instantiate my objects and I already provided a minimal example on the GitHub link. – Victorqedu Apr 12 '23 at 13:40
  • The error comes from the code, I used Java 19 to compile, could this be the cause? – Victorqedu Apr 12 '23 at 13:41
  • Mark Rotteveel, I recompiled everything in java 17 and the result is the same, the error appears on the Github project, you obviously didn't read my post... – Victorqedu Apr 12 '23 at 13:48
  • 2
    The exception mentions that the NPE is produced by `com.example.demo.Subject.getText()` on `this.subject` on line 16 of `SearchBase.java`. The code shown does not contain a `getText()`, nor a field called `subject`, and it is also not defined in a file called `SearchBase.java` (the referenced class must be defined in a file called `TestRestController.java` as it is a public class). In other words, it doesn't happen in the code shown, but in code you don't show. – Mark Rotteveel Apr 12 '23 at 14:00
  • 1
    And to be clear, questions must be self-contained, that means the [mre] must be in the question itself, not linked off-site. – Mark Rotteveel Apr 12 '23 at 14:05
  • In my first test, I didn't mentioned the input string because it was none, the assumption that I used an input string is wrong... it's just my opinion that this kind of voting is not justified. Mark simply added something from his imagination to my question that was not there and so reached the wrong conclusion. :)) – Victorqedu Apr 12 '23 at 15:00
  • I never asked for the input string, and I didn't _"add[..] something from [my] imagination"_. I asked for a [mre] that includes the code identified in the exception message and the only line of the stacktrace you included. – Mark Rotteveel Apr 13 '23 at 07:26
  • Ok, as I said yesterday, the problem is solved, is hard to communicate when my messages are deleted for no reason and other are changed... Thank you. – Victorqedu Apr 13 '23 at 13:35

1 Answers1

1

your NPE has nothing to do with autowiring. exception is triggered from the parameter @RequestBody SearchTest object which is parsed from the request body.

Try to send a POST request with the following body:

{
    "subject" : {
        "text": "TEST" 
    }
}

And debug received/parsed object parameter in IDE.

ursa
  • 4,404
  • 1
  • 24
  • 38
  • Thank you, this clarifies the cause of the problem as I tested initially with an empty JSON input , but why can't I have extra attributes in my class, attributes that are not in the JSON and that are autowired? – Victorqedu Apr 12 '23 at 14:25
  • 1
    these are different parts of the Spring - request parsing and IoC injections. but you can implement your own `org.springframework.core.convert.converter.Converter` utilising some autowired beans. see for example https://www.baeldung.com/spring-mvc-custom-data-binder – ursa Apr 12 '23 at 14:41