setImage ImageView在initialize()之外NullPointerException

时间:2018-11-10 20:54:08

标签: java image javafx fxml scenebuilder

我是新的javafx用户。我正在尝试创建一个基本的应用程序,但是遇到了一些问题。我试图在关闭子窗口时通过更改ImageView中的Image来更改按钮的图标。

我有我的主窗口:

<AnchorPane id="AnchorPane" prefHeight="651.0" prefWidth="823.0" 
   xmlns="http://javafx.com/javafx/10.0.1" 
   xmlns:fx="http://javafx.com/fxml/1" 
     fx:controller="amzevent.UI.FXMLMainUI">
    <children>
      <ScrollPane layoutX="14.0" layoutY="110.0" prefHeight="526.0" 
       prefWidth="796.0" />
        <ToolBar layoutX="14.0" layoutY="14.0" prefHeight="46.0" 
         prefWidth="796.0">
          <items>
            <Button minHeight="40.0" minWidth="40.0" mnemonicParsing="false" 
             prefHeight="34.0" prefWidth="40.0">
           <graphic>
              <ImageView fitHeight="30.0" fitWidth="30.0" 
               pickOnBounds="true" preserveRatio="true" 
               fx:id="bouton_retour">
                 <image>
                    <Image url="@../../resources/undo (1).png"   />
                 </image>
              </ImageView>
           </graphic>
           <tooltip>
              <Tooltip text="Annuler" />
           </tooltip>
        </Button>
     </items>
  </ToolBar>
  </children>
  </AnchorPane>

和控制器:

public class FXMLMainUI extends Controlleur implements Initializable {



@FXML 
private ImageView bouton_retour; 

@FXML
protected void ClickBoutonCreerSalle(ActionEvent event) {
    System.out.println("JavaMainUI : Click bouton créer salle");
    try {

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLCreerSalle.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        this.salle = new Stage();
        this.salle.setTitle("Nouvelle Salle");
        this.salle.setScene(new Scene(root1));
        this.salle.show();
    } catch (IOException ex) {
        Logger.getLogger(FXMLMainUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    /*FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLMainUI.fxml"));
    try {
        Parent root = loader.load();
    } catch (IOException ex) {
        Logger.getLogger(FXMLMainUI.class.getName()).log(Level.SEVERE, null, ex);
    }*/
}


protected void CreerSalle(String nom_salle, int largeur_salle, int profondeur_salle) {
    super.CreerSalle(nom_salle, largeur_salle, profondeur_salle);
    System.out.println("JavaMainUI : Créer salle");
}


@FXML
protected void MettreBoutonCliquables() {
    System.out.println("JavaMainUI : MettreBoutonCliquables");
    Image img = new Image("resources/undo.png");
    this.bouton_retour.setImage(img);
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    assert this.bouton_retour != null : "fx:id=\"bouton_retour\" was null check your FXML "; 
    this.bouton_retour = new ImageView();
    System.out.println("JavaMainUI : Initialize ");
}    
public FXMLMainUI()
{
}

}

其他视图:

   <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
   xmlns="http://javafx.com/javafx/10.0.1" 
   xmlns:fx="http://javafx.com/fxml/1" 
   fx:controller="amzevent.UI.FXMLCreerSalle">
    <children>
    <Button layoutX="486.0" layoutY="326.0" mnemonicParsing="false" 
       text="Button" onAction="#SoumettreNouvelleSalle" />
    <Label layoutX="118.0" layoutY="132.0" prefHeight="17.0" 
     prefWidth="59.0" text="Nom" />
     <Label layoutX="116.0" layoutY="183.0" prefHeight="17.0" 
   prefWidth="65.0" text="Largeur" />
    <TextField layoutX="208.0" layoutY="128.0" fx:id="field_nom"/>
    <TextField layoutX="208.0" layoutY="179.0" fx:id="field_largeur"/>
    <TextField layoutX="208.0" layoutY="233.0" fx:id="field_profondeur"/>
    <Label layoutX="116.0" layoutY="237.0" prefHeight="17.0" 
     prefWidth="65.0" 
  text="Profondeur" />
    <Label layoutX="363.0" layoutY="183.0" text="en metre" />
    <Label layoutX="363.0" layoutY="237.0" text="en metre" />
  </children>
  </AnchorPane> 

另一个控制器:

  public class FXMLCreerSalle extends FXMLMainUI implements Initializable {

@FXML
private TextField field_nom;

@FXML
private TextField field_largeur;

@FXML
private TextField field_profondeur;



@FXML
protected void SoumettreNouvelleSalle(ActionEvent event) {
    System.out.println("JavaMainUI : Soumettre nouvelle salle");
    if (!this.field_nom.getText().equals("") && !this.field_largeur.getText().equals("") && !this.field_profondeur.getText().equals("")) {
        String nom_salle = this.field_nom.getText();
        int largeur_salle = 0;
        int profondeur_salle = 0;
        try {
            largeur_salle = Integer.parseInt(this.field_largeur.getText());
            profondeur_salle = Integer.parseInt(this.field_profondeur.getText());
        }  catch (NumberFormatException e) {
            System.out.println("JavaMainUI : largeur et profondeur doivent etre des nombre entiers"); 
            return;
        }
        System.out.println("JavaMainUI : " + nom_salle); 
        System.out.println("JavaMainUI : " + largeur_salle);
        System.out.println("JavaMainUI : " + profondeur_salle);
        Stage stage = (Stage) this.field_nom.getScene().getWindow();
        // do what you have to do
        stage.close();
        super.CreerSalle(nom_salle, largeur_salle, profondeur_salle);
        super.MettreBoutonCliquables();
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {

    // TODO
    //retour_couleur = new Image("resources/undo.png");
    //this.retour = new ImageView();
    //this.boutton_retour = new ImageView();
}    

}

当子窗口关闭时,它将调用super.MettreBoutonCliquables();。用新的源更改ImageView的图像。

当我运行程序时,一切正常,直到this.bouton_retour.setImage(img);。 -> java.lang.NullPointerException

我尝试添加以下几行以重新加载我的fxml主文件,但未进行任何更改。 FXMLLoader loader =新的FXMLLoader(getClass()。getResource(“ / amzevent / UI / FXMLMainUI.fxml”)));         父根= loader.load();

在我的初始化功能中,bouton_retour不为空。

我不知道如何解决此问题,如果以前有人遇到过这种问题,那么我就是接受者。

0 个答案:

没有答案