参见英文答案 > Non-static variable cannot be referenced from a static context 12个
我的Spring-Boot应用程序中有2个类:
-任务
-Runner
runner类包含我的main方法,我尝试从Tasks类调用一个方法:
亚军:
@Component
public class Runner {
Tasks tasks;
@Autowired
public void setTasks(Tasks tasks){
this.tasks=tasks;
}
public static void main(String[] args){
//error being caused by below line
tasks.createTaskList();
}
任务类:
@Service
public class Tasks {
public void createTaskList() {
//my code
}
//other methods
}
在我的Runner中,当我尝试在Tasks类中调用createTaskList()方法时,我收到以下错误:
Non static field 'tasks' cannot be referenced from a static context
我怎么解决这个问题?
最佳答案