将JavaFX imageview添加到StackPane时将引发null

时间:2019-02-28 15:50:29

标签: java javafx

我有一个相当复杂的程序,应该修改。

这里有一个MainWindow,它包裹了应用程序的各个组件并将它们显示为UI。

MainWindow.java:

// Independent Ui parts residing in this Ui container
private BrowserPanel browserPanel;
..
@FXML
private StackPane browserPlaceholder;
..
void fillInnerParts(){
  browserPanel = new BrowserPanel(logic.selectedPersonProperty());
  browserPlaceholder.getChildren().add(browserPanel.getRoot());
  ..
}

BrowserPanel.java:

 /**
 * The Browser Panel of the App.
 */
public class BrowserPanel extends UiPart<Region> {

   ..

    private static final String FXML = "BrowserPanel.fxml";

    @FXML
    private WebView browser;

    public BrowserPanel(ObservableValue<Person> selectedPerson) {
        super(FXML);
        ..  
    }
 ..

BrowserPanel.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.web.WebView?>

<StackPane xmlns:fx="http://javafx.com/fxml/1">
  <WebView fx:id="browser"/>
</StackPane>

我尝试修改此浏览器类以显示图像。

MainWindow.java

// Independent Ui parts residing in this Ui container
private ImagePanel imagePanel;
..
@FXML
private StackPane ImagePanelPlaceholder;
..
void fillInnerParts(){
  imagePanel = new ImagePanel();
  imagePanelPlaceholder.getChildren().add(imagePanel.getRoot());
  ..
}

ImagePanel.java

public class ImagePanel extends UiPart<Region>{
    private static final String FXML = "ImagePanel.fxml";

    @FXML
    private ImageView imageView;

    public ImagePanel(){
        super(FXML);
        Image sample = new Image("/images/sample.png");
        imageView.setImage(sample);
    }
}

ImagePanel.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.ImageView?>


<ImageView fx:id="imageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" />

但是,我得到了错误:

SEVERE: Fatal error during initializing nullnull
java.lang.NullPointerException
    at seedu.address.ui.MainWindow.fillInnerParts(MainWindow.java:120)

其中fillInnerParts是将所有组件添加到MainWindow中的方法,如上所示。

我将如何修改它以便可以在JavaFX应用程序上显示图片?

0 个答案:

没有答案