title: @Autowired dependency injection returns null pointer for static field
body: <p>I'm working on a project in which I'm trying to inject dependency into a class but <code>@Autowired</code> annotating does not seem to work for some reason.</p>
  
  <p>I have the class <strong>ApplicationServer</strong> </p>
  
  <pre><code>package project.eHealth;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.HashSet;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.stereotype.Component;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  import project.eHealth.bussiness.AppointmentDto;
  import project.eHealth.bussiness.AppointmentService;
  
  @SpringBootApplication
  @Component
  public class ApplicationServer {
  
      private static final int PORT = 9001;
  
      private static HashSet&lt;String&gt; names = new HashSet&lt;String&gt;();
  
      @Autowired
      private static AppointmentService appointments;
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(ApplicationServer.class, args);
          System.out.println("The chat server is running.");
          ServerSocket listener = new ServerSocket(PORT);
          try {
              while (true) {
                  new Handler(listener.accept()).start();
              }
          } finally {
              listener.close();
          }
  
      }
  
          private static class Handler extends Thread {
  
              private String name;
              private Socket socket;
              private BufferedReader in;
              private PrintWriter out;
  
  
  
  
              /**
               * Constructs a handler thread, squirreling away the socket.
               * All the interesting work is done in the run method.
               */
              public Handler(Socket socket) {
                  this.socket = socket;
              }
  
              /**
               * Services this thread's client by repeatedly requesting a
               * screen name until a unique one has been submitted, then
               * acknowledges the name and registers the output stream for
               * the client in a global set, then repeatedly gets inputs and
               * broadcasts them.
               */
              public void run() {
  
  
                  try {
  
                      // Create character streams for the socket.
                      in = new BufferedReader(new InputStreamReader(
                          socket.getInputStream()));
                      out = new PrintWriter(socket.getOutputStream(), true);
                      ObjectMapper mapper = new ObjectMapper();
  
  
                      // Request a name from this client.  Keep requesting until
                      // a name is submitted that is not already used.  Note that
                      // checking for the existence of a name and adding the name
                      // must be done while locking the set of names.
                      /*while (true) {
                          out.println("SUBMITNAME");
                          name = in.readLine();
                          if (name == null) {
                              return;
                          }
                          synchronized (names) {
                              if (!names.contains(name)) {
                                  names.add(name);
                                  break;
                              }
                          }
                      }*/
  
                      // Now that a successful name has been chosen, add the
                      // socket's print writer to the set of all writers so
                      // this client can receive broadcast messages.
  
  
  
                      // Accept messages from this client and broadcast them.
                      // Ignore other clients that cannot be broadcasted to.
                      while (true) {
  
                          System.out.println("Aici este controlul!");
                          String input = in.readLine();
                          System.out.println(input);
  
                          Request request = mapper.readValue(input,Request.class);
  
                          if (request.getCommand().equals("getAllAppointments")){
  
                          if (appointments == null){
                              System.out.println("appointmentService is null");
                          }
                          else{
                                  List&lt;AppointmentDto&gt; appointmentsList = appointments.getAll();
                                  Response response = new Response();
                                  response.setAction("RETURNED");
                                  response.setData(mapper.writeValueAsString(appointmentsList));
                                  String respObj = mapper.writeValueAsString(response);
                                  out.println(respObj);
                          }   
  
  
                          }
  
                          System.out.println(input);
                          if (input == null) {
                              return;
                          }
  
                         String[] command = input.split("&amp;");
  
                         }
  
                  } catch (IOException e) {
                      System.out.println(e);
                  } finally {
                      // This client is going down!  Remove its name and its print
                      // writer from the sets, and close its socket.
                      if (name != null) {
  
                      }
                      if (out != null) {
  
                      }
  
                  }
              }
  
  
  
  
      }
      }
  </code></pre>
  
  <p>And the class <strong>AppointmentService</strong> </p>
  
  <pre><code>package project.serverSide.server.bussiness;
  
  
  
  import java.time.LocalDate;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Service;
  
  import javafx.beans.property.SimpleObjectProperty;
  import javafx.beans.property.SimpleStringProperty;
  import project.serverSide.server.data.Appointment;
  import project.serverSide.server.data.AppointmentRepository;
  
  
  @Component
  @Service
  public class AppointmentService {
  
      @Autowired
      AppointmentRepository appointments;
  
      public AppointmentService(){
  
      }
  
      public List&lt;AppointmentDto&gt; getAll(){
  
          List&lt;Appointment&gt; appointmentsList = appointments.findAll();
          List&lt;AppointmentDto&gt; appointmentsDto = new ArrayList&lt;AppointmentDto&gt;();
  
          for (Appointment ap : appointmentsList){
              AppointmentDto apDto = new AppointmentDto();
              apDto.setDoctorName(new SimpleStringProperty(ap.getDoctorName()));
              apDto.setPatientName(new SimpleStringProperty(ap.getPatientName()));
              apDto.setDateIssued(new SimpleObjectProperty&lt;LocalDate&gt;(ap.getDateIssued()));
              appointmentsDto.add(apDto);
          }
  
          return appointmentsDto;
      }
  }
  </code></pre>
  
  <p>And <strong>AppointmentRepository</strong></p>
  
  <pre><code>  package project.serverSide.server.data;
  
  
      import java.util.List;
  
      import org.springframework.data.jpa.repository.JpaRepository;
      import org.springframework.stereotype.Component;
  
      @Component
      public interface AppointmentRepository extends JpaRepository&lt;Appointment,Integer&gt; {
          List&lt;Appointment&gt; findAll();
      }
  </code></pre>
  
  <p>The issue is that <strong>ApplicationServer</strong> class does not inject the <strong>AppointmentService</strong> for some reason and I do not know why. I've been spending an important number of hours search for an answer but without any result.</p>
  
  <p>Help is deeply appreciated!</p>