-2

i have the 3 classes:

 public abstract class Provider{
   @Autowired
   private ModelService modelService;

   protected void foo(String name) throws Exception {
       modelService.doSomething();   //line 10
   }
 }


 @Service
 public class MyProvider extends Provider {
       // calling foo on the parent
       foo("mayName");
 }

 public class MyOAuthProvider extends MyProvider {
       // calling foo on the parent-->parrent
       foo("mayName");
 }
  • when i call foo from MyProvider the function work

  • when i call foo from MyOAuthProvider the function fail on NullPointerException because modelService is null

trace:

Exception in thread "main" java.lang.NullPointerException
    at my.Provider.foo(Provider.java:10)
    at my.Nain.main(Nain.java:7)

Anyone can help me to understand why this happens and how can i solve my problem

Yosefarr
  • 709
  • 5
  • 12
  • 26

1 Answers1

-1

annotate MyOAuthProvider with @service will solve you issue

sk1030
  • 52
  • 4
  • i annotate MyOAuthProvider with @service and still get the same problem – Yosefarr Jun 03 '20 at 17:00
  • how do you create `MyOAuthProvider`? if you create it with `new`, your autowiring will not work. in order for the autowire to work, you need spring to create it for you, by using `@Service` and componentScan – Nir Levy Jun 03 '20 at 21:24