I want to connect the bot to the database, but every time I try to do this, an error crashes, and I have never been able to figure out what the problem is(
Console
2022-11-06 21:02:47.397 ERROR 12320 --- [legram Executor] o.t.t.u.DefaultBotSession : Cannot invoke "site.ewrey.RocketTestingSpringBot.JPA.UserRepo.save(Object)" because "this.repo" is null
java.lang.NullPointerException: Cannot invoke "site.ewrey.RocketTestingSpringBot.JPA.UserRepo.save(Object)" because "this.repo" is null
at site.ewrey.RocketTestingSpringBot.JPA.UserServiceImpl.save(UserServiceImpl.java:14) ~[classes/:na]
at site.ewrey.RocketTestingSpringBot.settings.CommandsBot.onUpdateReceived(CommandsBot.java:69) ~[classes/:na]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) ~[telegrambots-meta-6.1.0.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:317) ~[telegrambots-6.1.0.jar:na]
AppConfig Class
package site.ewrey.RocketTestingSpringBot;
@Configuration
@EnableJpaRepositories(basePackages = "site.ewrey.RocketTestingSpringBot.JPA")
@ComponentScan("site.ewrey.RocketTestingSpringBot")
public class AppConfig {
@Autowired
UserRepo repo;
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public void registrationBot(){
CommandsBot bot = new CommandsBot();
try {
new TelegramBotsApi(DefaultBotSession.class).registerBot(bot);
System.out.println("Бот @"+bot.getBotUsername()+" успешно запущен!!!");
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
}
Service interfaceImpl
package site.ewrey.RocketTestingSpringBot.JPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepo repo;
@Override
public void save(UserEntity entity) {
repo.save(entity);
}
}
Commands class
package site.ewrey.RocketTestingSpringBot.settings;
@Component
public class CommandsBot extends TelegramLongPollingBot {
@Autowired
private UserServiceImpl service = new UserServiceImpl();
@Override
public String getBotUsername() {
return "NAME";
}
@Override
public String getBotToken() {
return "TOKEN";
}
@Override
public void onUpdateReceived(Update update) {
long chatId = update.getMessage().getChatId();
boolean hasText = update.hasMessage() && update.getMessage().hasText();
String command = update.getMessage().getText();
if (hasText && command.equals("/addMe")){
UserEntity entity = new UserEntity();
entity.setChatId(chatId);
entity.setSubscribe(false);
service.save(entity);
sendMessage(update,"That's OK!");
}
}
public void sendMessage(Update update, String text){
try {
execute(
SendMessage.builder()
.chatId((update.hasMessage()) ? update.getMessage().getChatId() : update.getCallbackQuery().getFrom().getId())
.parseMode("Markdown")
.text(text)
.build());
}
catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
I see this problem any times, when i don't use a JPA into MVC, and i really don't understand what's wrong