Jrawr Jrawr - 12 days ago 8
Java Question

java.lang.NullPointerException for static button in JavaFX with static reference

Okay so I have a bit a conundrum. I have a Qaurtz Cron that I want to use to schedule and run some Java Tests. These task are scheduled through a gui that Uses JavaFX. However, the job itself calls a run tests method. The job forces me to make certain elements static, but by making them static, I get a null pointer exception. I would really appreciate some help here.

So here is the job class that forces things to be static.

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();
}
}
}


Inside my controller I have something like this:

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);


Note that the runScheduledTests() methods does some configuring and calls the runTest method. Inside the run test method is where I'm hitting the error specifically this line:

if (IEbutton.isSelected()) {
BrowserConfig.setDriver(Browsers.IE());
} else {
BrowserConfig.setDriver(Browsers.Chrome());
}


The reason is that above I have this :

@FXML
public static RadioButton ChromeButton;

@FXML
public static RadioButton IEbutton;


As I said this is a bit of an issue, If I don't make them static the job class yells at me for making a non-static reference.

How can I resolve this conflict?

Answer

TL;DR : You shouldn't use static on the fields annotated with @FXML.

For more information go through - javafx 8 compatibility issues - FXML static fields

You could load the FXML by using the FXMLLoader and then get an instance of the controller from it. By doing this, you can convert the method runScheduledTests() into a non-static method.

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();
        }
    }
}