У меня есть проект maven с Spring / Hibernate, и я получаю исключение NullPointerException на this.userService.save(user);
.UserService является @Autowired, поэтому я не уверен, почему это выбрасывает NPE.Пример кода с UserService:
@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); // <-- NPE
}
}
UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.model.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
User findById(int id);
}
UserService.java
import com.example.model.User;
public interface UserService {
public User findById(int id);
public void save(User user);
}
UserServiceImpl.java
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);
}
}
UPD.Добавлено подробное описание
BotExampleApplication.java:
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);
}
}
stacktrace:
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]
Любые предложения