title: UserService NullPointerException Spring/Hibernate
body: <p>I have a maven project with Spring/Hibernate and I keep getting NullPointerException at <code>this.userService.save(user);</code>. UserService is @Autowired, so I'm not sure why this throws NPE. 
  code sample with UserService:</p>
  
  <pre><code>@Component
  public class BotService extends TelegramLongPollingBot {
  
      @Autowired
      private UserService userService;
  
      @Autowired
      private MessagesService messagesService;
  
      @Override
      public void onUpdateReceived(Update update) {
          Message message = update.getMessage();
          String text = message.getText();
          User user = new User();
          user.setTelegramId(message.getFrom().getId());
          user.setFirstName(message.getFrom().getFirstName());
          user.setLastName(message.getFrom().getLastName());
          Messages messages = new Messages();
          messages.setUser(user);
          messages.setId(user.getId());
          messages.setText(text);
          this.userService.save(user); // &lt;-- NPE 
          }
      }
  </code></pre>
  
  <p>UserRepository.java</p>
  
  <pre><code>import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  import com.example.model.User;
  
  @Repository
  public interface UserRepository extends JpaRepository&lt;User, Long&gt;{
      User findById(int id);
  }
  </code></pre>
  
  <p>UserService.java</p>
  
  <pre><code>import com.example.model.User;
  
  public interface UserService {
      public User findById(int id);
      public void save(User user);
  }
  </code></pre>
  
  <p>UserServiceImpl.java</p>
  
  <pre><code>import javax.transaction.Transactional;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import com.example.model.User;
  import com.example.repository.UserRepository;
  
  @Service("userService")
  public class UserServiceImpl implements UserService{
  
      @Autowired
      private UserRepository userRepository;
  
      @Override
      @Transactional
      public void save(User user) {
          userRepository.save(user);
      }
  
      @Override
      public User findById(int id) {
          return userRepository.findById(id);
      }
  }
  </code></pre>
  
  <p>UPD. Added detailed description</p>
  
  <p>BotExampleApplication.java:</p>
  
  <pre><code>import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.telegram.telegrambots.ApiContextInitializer;
  import org.telegram.telegrambots.TelegramBotsApi;
  import org.telegram.telegrambots.exceptions.TelegramApiException;
  import org.telegram.telegrambots.starter.EnableTelegramBots;
  
  import com.example.service.BotService;
  
  @SpringBootApplication
  @EnableTelegramBots
  @EnableTransactionManagement
  public class BotExampleApplication {
  
      public static void main(String[] args) {
          ApiContextInitializer.init();
  
          TelegramBotsApi botsApi = new TelegramBotsApi();
  
          try {
              botsApi.registerBot(new BotService());
          } catch (TelegramApiException e) {
              e.printStackTrace();
          }
          SpringApplication.run(BotExampleApplication.class, args);
      }
  }
  </code></pre>
  
  <p>stacktrace:</p>
  
  <pre><code>java.lang.NullPointerException: null
      at com.example.service.BotService.onUpdateReceived(BotService.java:46) ~[classes/:na]
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1380) ~[na:na]
      at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) ~[telegrambots-meta-3.6.1.jar:na]
      at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309) ~[telegrambots-3.6.1.jar:na]
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OshzL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OshzL.jpg" alt="Package structure"></a></p>
  
  <p>Any suggestions?</p>