我得到了具有以下继承结构的任务对象:
我该怎么做?通过以下代码,我得到了一个空指针异常。
家长:
public abstract class ParentTask implements Runnable {
// some fields
protected ParentTask(/*fields*/) {
//this.fields = fields;
}
@Override
public abstract void run();
}
第一个孩子:
public class SystemTask extends ParentTask {
@Autowired
protected SomeService someService;
protected SystemTask(/*fields*/) {
super(fields);
//set some other fields
}
@Override
public void run() {
someService.doSomething(); // <-- nullPointerException
}
}
public abstract class UserTask extends ParentTask {
@Autowired
protected SomeService someService;
@Autowired
protected SomeRepository someRepository;
protected UserTask(/*fields*/) {
super(fields);
}
@Override
public abstract void run();
}
UserTask的子级(应具有UserTask的自动连接资源)
public class SpecificUserTask extends UserTask{
private SpecificUserTask (/*fields*/) {
super(fields);
//set some other fields
}
@Override
public void run() {
// do something
}
}
我的存储库是一个标准的mongo存储库,不应该有任何错误。我的“someService”看起来像这样:
@Service
public class SomeService{
// some fields
@Autowired
private someOtherRepository someOtherRepository;
// some methods
public void doSomething() {
//...
}
}
-
如何解决NullPointerException?
-
如何自动连接UserTask中的字段,使其在SpecificUserClass中可用?
提前谢谢大家。
@Service
public class TaskAdministration {
private final TaskAdministrationRepository taskAdministrationRepository;
public void commissionSystemTask(fields) {
SystemTask task = new SystemTask(fields);
taskScheduler.schedule(task, taskTrigger);
// persist task to restore if necessary
taskAdministrationRepository.save(task);
}
}