title: Error creating bean while using Autowired
body: <p>I have a simple Springboot application in which I am trying to use in memory HashMap as a DB to store some info.
  This is my restcontroler</p>
  <pre><code>    @RestController
  public class Parking {
  
      @Autowired
      ParkingServices parkingServices;
  
      @Autowired
      Vehicle vehicle;
  
      @GetMapping(&quot;/test&quot;)
      public void park() {
          vehicle = Vehicle.builder().color(&quot;Blue&quot;).plateNumber(&quot;qwew&quot;).build();
          parkingServices.parkCar(vehicle);
      }
  
      @GetMapping(&quot;/&quot;)
      public void Test() {
          System.out.println(&quot;test&quot;);
      }
  }
  </code></pre>
  <p>This is my simple parking service</p>
  <pre><code>   @Service
  public class ParkingServices {
  
      @Autowired
      ParkingLotService parkingLot;
  
      public ParkingServices() {
          parkingLot.init(10);
      }
  
      public int parkCar(Vehicle vehicle) {
          return parkingLot.parkCar(vehicle);
      }
  }
  </code></pre>
  <p>And this is my ParkingLot service</p>
  <pre><code>  @Service
  public class ParkingLotService {
      private int size = 0;
      private HashMap&lt;Integer, Slot&gt; parkingSlots = new HashMap&lt;&gt;();
      private Stack&lt;Integer&gt; st = new Stack&lt;&gt;();
  
      public void init(int size) {
          this.size = size;
          parkingSlots = new HashMap&lt;&gt;();
          for (int i = 0; i &lt; size; i++) {
              parkingSlots.put(i, new ParkingSlot());
              st.push(i);
          }
      }
  
      public int parkCar(Vehicle vehicle) {
          int firstPark = st.pop();
          Slot slot = parkingSlots.get(firstPark);
          slot.parkVehicle(vehicle);
          parkingSlots.put(firstPark, slot);
          return firstPark;
      }
  
      public void removeCar(int parkingNumber) {
          Slot slot = parkingSlots.get(parkingNumber);
  
      }
  }
  </code></pre>
  <p>Now when i try to run the application I am getting this error as a stack trace</p>
  <pre><code>    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parking': Unsatisfied dependency expressed through field 'parkingServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parkingServices' defined in file [D:\Projects\parking\demo\target\classes\com\parking\demo\Services\ParkingServices.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.parking.demo.Services.ParkingServices]: Constructor threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.5.jar:5.3.5]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.5.jar:5.3.5]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) [spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313) [spring-boot-2.4.4.jar:2.4.4]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) [spring-boot-2.4.4.jar:2.4.4]
      at com.parking.demo.ParkingApplication.main(ParkingApplication.java:11) [classes/:na]
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parkingServices' defined in file [D:\Projects\parking\demo\target\classes\com\parking\demo\Services\ParkingServices.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.parking.demo.Services.ParkingServices]: Constructor threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1316) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.5.jar:5.3.5]
      ... 21 common frames omitted
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.parking.demo.Services.ParkingServices]: Constructor threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:225) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.3.5.jar:5.3.5]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.3.5.jar:5.3.5]
      ... 32 common frames omitted
  Caused by: java.lang.NullPointerException: null
      at com.parking.demo.Services.ParkingServices.&lt;init&gt;(ParkingServices.java:15) ~[classes/:na]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_252]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_252]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_252]
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_252]
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:212) ~[spring-beans-5.3.5.jar:5.3.5]
      ... 34 common frames omitted
  </code></pre>
  <p>I know its due to Autowired is not working on ParkingLotService, its ability to work if instead of Autowired in ParkingService, I directly try to initialize Object but not the other way around.
  Can someone help me with it ? Why Autowired is not working and how to make it work.</p>