gpt4 book ai didi

JavaFX 中具有静态引用的静态按钮的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 09:13:06 26 4
gpt4 key购买 nike

好吧,我有一个难题。我有一个 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();
}
}
}

在我的 Controller 中我有这样的东西:

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;

正如我所说,这是一个问题,如果我不将它们设为静态,作业类就会因为我进行非静态引用而对我大喊大叫。

如何解决此冲突?

最佳答案

TL;DR:您不应在使用 @FXML 注释的字段上使用 static。

欲了解更多信息,请访问 - javafx 8 compatibility issues - FXML static fields

您可以使用 FXMLLoader 加载 FXML,然后从中获取 Controller 的实例。通过这样做,您可以将方法 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();
}
}
}

关于JavaFX 中具有静态引用的静态按钮的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com