我发现了这样的问题: 有一个MainWindow和2个SubWindows,但我有例外:
-Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
-Caused by: java.lang.NullPointerException: Location is required.
-[...]
-at ApplicationForTests.SubWin1.SubWin1.display(SubWin1.java:14)
> Parent root = FXMLLoader.load(getClass().getResource("ApplicationForTests/SubWin1UI.fxml"));
-[...]
-at ApplicationForTests.MainController.openSub1(MainController.java:22)
> subWin1.display();
-[...]
-Exception in thread "Thread-3" java.lang.IncompatibleClassChangeError
public class Main extends Application {
public static BdcsDAO db = new BdcsDAO();
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("MainUI.fxml"));
primaryStage.setTitle("Main window");
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
db.connect();
}
public static void main(String[] args) {launch(args);}
public class MainController {
@FXML
Button button1;
@FXML
Button button2;
@FXML
public void openSub1() {
SubWin1 subWin1 = new SubWin1();
try {
subWin1.display();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void openSub2() {
SubWin2 subWin2 = new SubWin2();
try {
subWin2.display();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ApplicationForTests.MainController">
<children>
<Button fx:id="button1" onAction="#openSub1" layoutX="268.0" layoutY="185.0" mnemonicParsing="false" text="SubWin1" />
<Button fx:id="button2" onAction="#openSub2" layoutX="268.0" layoutY="285.0" mnemonicParsing="false" text="SubWin2" />
</children>
</AnchorPane>
public class SubWin1 {
public void display() throws IOException {
Stage window = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("ApplicationForTests/SubWin1UI.fxml"));
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Sub window no. 1");
window.setScene(new Scene(root, 800, 500));
window.show();
}
}
public class SubWin1Controller {
@FXML
Button button;
@FXML
public void loadTableView() throws SQLException {
setAlarmsTableView(Main.db.getSampleResultSet());
}
@FXML
private TableView<AlarmType> alarmsTableView;
@FXML
private TableColumn<?, ?> columnDeviceIndex;
@FXML
private TableColumn<?, ?> columnAlgorithmType;
private ObservableList<AlarmType> dataAlarms = FXCollections.observableArrayList();
public void setAlarmsTableView(ResultSet rs) throws SQLException {
columnDeviceIndex.setCellValueFactory(new PropertyValueFactory<>("deviceIndex"));
columnAlgorithmType.setCellValueFactory(new PropertyValueFactory<>("algorithmType"));
while (rs.next()) {
dataAlarms.add(new AlarmType(rs.getInt(1), rs.getInt(2));
}
alarmsTableView.setItems(dataAlarms);
}
}