Search code examples
javaexceptionfxml

IOEXCEPTION error again and again in FXML . Trying to put FXML file on a list element in ListView


public class PopupController  {
    public ListView<String> listView;
    public Button addWalletButton;
    public PieChart piechart;
    public Label size;
    private WalletModel walletModel = Factory.inject(WalletModel.class);

    @FXML
    public void initialize() throws IOException {


        listView.setCellFactory(param -> new EditableCell());
        addWalletButton.setOnMouseClicked(event -> {
            walletModel.CreateWallet();
            listView.getFixedCellSize();
            listView.getItems().add("Wallet " + walletModel.WalletSize());
            size.setText("Total Wallets:  " + walletModel.WalletSize());
        });
        size.setText("Wallet Size " + walletModel.WalletSize());
        listView.getItems().add("Wallet 1");
    }

    private class EditableCell extends ListCell<String>{

        private final TextField textField;

        EditableCell() throws IOException {
            textField = new TextField();
            setGraphic(FXMLLoader.load(getClass().getResource("/selectbutton.fxml")));
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if(empty){
                textField.setVisible(false);
            }
            else{
                textField.setVisible(true);
                textField.setText(item);
            }
        }
    }



}

Its showing error in the first statement in initialize() method.

I am trying to put a fxml file on a list element by button ("add wallet"). I have attached my fxml code below.

I am not receiving the stacktrace , because it shows compilation error

<AnchorPane prefHeight="27.0" prefWidth="69.0" xmlns="http://javafx.com/javafx/8.0.121"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gazman.coco.desktop.controllers.PopupController">
    <Button fx:id="select" layoutX="6.0" layoutY="2.0" mnemonicParsing="false" text="Button"/>
</AnchorPane>

Solution

  • IOException is a checked exception, which means it has to be explicitely handled in either of two ways:

    • caught in the catch clause of a try-catch statement
    • propagated by the method where the exeption might happen. This is done by adding throws IOException to the end of the signature of your method.

    This is for forcing developers to handle typical failures for IO and other operations with a reasonable expectation of external failure.