我有一个使用Spring / Hibernate的maven项目,并且我在this.userService.save(user);
处继续获得NullPointerException。 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);
}
}
堆栈跟踪:
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]
有什么建议吗?
答案 0 :(得分:1)
您永远不应该创建弹簧组件的新实例。在您的主要课程new
中使用BotService
创建。这与spring创建的@Autowired
实例不同。 BotService
仅适用于spring组件,并且因为您使用的是另一个实例,所以依赖项永远不会注入其中。
要使其工作,请从spring上下文中获取ConfigurableApplicationContext context = SpringApplication.run(...);
BotService service = context.getBean(BotService.class);
实例,然后使用它进行注册。
{{1}}
答案 1 :(得分:0)
你应该添加setter和getter
@Autowired
private UserRepository userRepository;
/* setter and getter forr user userRepository*/
等在另一个文件中
@Autowired
private UserService userService;
@Autowired
private MessagesService messagesService;
/*setters and getters here too */