好的,所以我有点难题。我有一个Qaurtz Cron,我想用它来安排和运行一些Java测试。这些任务是通过使用JavaFX的gui安排的。但是,作业本身调用运行测试方法。这个工作迫使我将某些元素设置为静态,但是通过使它们成为静态,我得到一个空指针异常。我真的很感激这里的一些帮助。
所以这里的工作类强制事物是静态的。
public class newJob implements Job{
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("We are attempting the job now");
try {
FXMLDocumentController.runScheduledTests();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在我的控制器中,我有类似的东西:
public static void runTests() throws SQLException, IOException {
// Set running to true. In current form we do not use this bool,
// however, we may
// make changes that rely on this.
running = true;
FileOutputStream fos = null;
// Verify that users exist, and there is a url with the word max in it
int count = DBHelpers.getResults("SELECT * FROM USERS;", new String[] { "ID" }).length();
// Verify that we have both users and a maximo url to work with.
if ((!userList.isEmpty() || count > 0) && userMaxListChoice.length() > 5) {
// Set the proper driver, default to chrome if none is selected
if (IEbutton.isSelected()) {
BrowserConfig.setDriver(Browsers.IE());
} else {
BrowserConfig.setDriver(Browsers.Chrome());
}
// Let's assign maximo. If no one has clicked the use UserList
// button, assume that the data inside
// maximo name is good to use
if (userMaxListChoice != null) {
BrowserConfig.setMaximo(userMaxListChoice);
// System.out.println("used maxLIst choice");
} else {
// If the user has not selected a name from the maximo list,
// let's grab whatever
// they have entered in the maximoName field.
BrowserConfig.setMaximo(maximoName.getText());
}
// Set the system pause based on the interval string
int pause = Integer.parseInt(interval.getText().toString());
// Make sure the puase is in miliseconds
pause = pause * 1000;
BrowserConfig.setInterval(pause);
请注意,runScheduledTests()方法会进行一些配置并调用runTest方法。在运行测试方法中,我特意在这一行点击错误:
if (IEbutton.isSelected()) {
BrowserConfig.setDriver(Browsers.IE());
} else {
BrowserConfig.setDriver(Browsers.Chrome());
}
原因是上面我有这个:
@FXML
public static RadioButton ChromeButton;
@FXML
public static RadioButton IEbutton;
正如我所说,这是一个问题,如果我不让它们变得静止,那么工作类会因为制作非静态参考而对我大吼大叫。
如何解决此冲突?
答案 0 :(得分:1)
TL; DR:您不应在使用@FXML注释的字段上使用static。
有关详细信息,请访问 - javafx 8 compatibility issues - FXML static fields
您可以使用FXMLLoader加载FXML,然后从中获取控制器的实例。通过这样做,您可以将方法runScheduledTests()
转换为非静态方法。
public class newJob implements Job{
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
FXMLLoader fxmlLoader =
new FXMLLoader(getClass().getResource("path-to-fxml.fxml"));
fxmlLoader.load();
FXMLDocumentController fxmlDocumentController =
(FXMLDocumentController)fxmlLoader.getController();
fxmlDocumentController.runScheduledTests(); // convert the method to non-static
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
问题是当控制器本身不是静态时,你试图使用静态。考虑到我建议看看静态和非静态here之间的区别。关于修复代码。首先让按钮ChromeButton
和IEbutton
不是静态的。然后当你的应用程序类,像这样:
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
//pass a reference of your stage to job.
}
}
然后将按钮或控制器的引用作为变量传递给Job类。像这样:
public class newJob implements Job{
private RadioButton chrome;
private RadioButton ie;
public newJob(RadioButton chrome, RadioButton ie) {
this.chrome = chrome;
this.ie = ie;
}
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("We are attempting the job now");
try {
FXMLDocumentController.runScheduledTests();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
您的问题的核心是控制器变量 CAN NOT 是静态的,因为控制器依赖于实例化类并传递所需的信息。解决此问题的最佳方法是将引用传递给通过在构造函数中使用变量来实现按钮或类。请阅读我发给您的链接,以便更好地了解静态和非静态。此外,如果您对构造函数感到困惑,请查看here。