-3

If I have a class variable foo, injected via Spring and I use foo to initialize another class variable like below, why does it throw a NullPointerException? However, if I perform the initialization in a method call of the class, it works fine.

class Baz {

   @In
   Object foo;

   Object bar = foo.someMethod();

}
Spock
  • 315
  • 2
  • 13
aakashdp
  • 81
  • 11

1 Answers1

0

Because you are using foo before Spring runs. Take a look at Java class initialization to understand it.

Grasshopper
  • 1,749
  • 1
  • 14
  • 30