我正在尝试运行以下但我在userService.getName()时遇到空指针; 没有线程它工作正常,但有线程它抛出空指针异常
public class Sheduler implements Callable{
private UserService userService;
private SchedulerExecuterService scheduler=Executers.newScheduledThreadpool(2);
@Inject
public Sheduler (UserService userService){
this.userService = userService;
}
Future future = scheduler.submit(new Callable(){
@override
public String call() throws Exception{
**return userService.getName();//NUll pointer Exception**
}
)}
public void startpool(){
final Runnable beeper = new Runnable(){
public void run(){
UserService userService = new UserService();
Sheduler scd=new Sheduler (UserService userService);
future.get();
}
}
scheduleAtFixedRate(beeper,5,5,TimeUnit.SECONDS);
}
public static void main(){
Sheduler scduler=new Sheduler();
scduler.startpool();
}
答案 0 :(得分:0)
您在@Inject
构造函数上使用Scheduler
但仍然手动实例化它。您的UserService
从未被注入,因此抛出NullPointerException
为空。