Spring bean field (which is instantiated explicitly) is assigned to null sometimes

257 views Asked by At

One day we faced an incident that application started crashing because of the NPE. I found the place where it was crashing, but I cannot be sure why does it happen.

Roughly speaking lets assume that I have a class A which appears a spring bean at the same time. This class has a field property which is instantiated explicitly (yes, I know how it sounds like) and a method propertyUser which uses this field:

class A {
   private ObjectClass property = new ObjectClass();
   public void propertyUser() {
      property.action();
   }   
   ....
   getter\setter for property
}

And after some time of usual work, property field has become null which has resulted in NPE from propertyUser method. I changed this legacy code and now this property is assigned via spring container either, however I do need to be sure that this was the reason of this error appearance.

So I would be very pleased if somebody could give me a hint.

0

There are 0 answers