を再起動した後にイメージを選択することができます。私のプログラムは、私がイメージを選択したChoiceBoxを持っていて、それを自動的にImageViewを自動的に更新しますが、イメージをExeceptionと呼ばれる新しいイメージに変更したい しかし、私はプログラムを再起動するときに問題なく新しいイメージを選択できます。私はプログラム(JAVA/FX)
mCb_img.getSelectionModel().selectedItemProperty().addListener(o -> {
mIv_img.setImage(new Image(getClass().getResource("/img/" + mCb_img.getSelectionModel().getSelectedItem()).toExternalForm()));
});
@FXML
private void changeOnAction(ActionEvent event) {
File img = null;
Path source = null;
Path target = null;
imgfileChooser.setTitle("Open Image File");
imgfileChooser.getExtensionFilters().add(
new FileChooser.ExtensionFilter("JPG", "*.jpg"));
img = imgfileChooser.showOpenDialog(new Stage());
source = img.toPath();
if (img != null) {
try {
File saved = new File(System.getProperty("user.dir") + "/src/im"
+ "g/" + source.getFileName());
target = saved.toPath();
Files.move(source, target);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
mCb_img.getItems().add(source.getFileName().toString());
// fillChoiceBox();
}
private void fillChoiceBox() {
mCb_img.getItems().clear();
try {
Files.walk(Paths.get(System.getProperty("user.dir") + "/src/img/"))
.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
mCb_img.getItems().add(filePath.getFileName()
.toString());
}
});
} catch (IOException ex) {
System.out.println("No files in IMG!");
}
mCb_img.getSelectionModel().select(0);
}
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at zweitehu.FXMLDocumentController.lambda$setListeners$1(FXMLDocumentController.java:248)
at zweitehu.FXMLDocumentController$$Lambda$219/404494396.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:223)
at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
at javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:402)
at com.sun.javafx.scene.control.skin.ChoiceBoxSkin.lambda$addPopupItem$266(ChoiceBoxSkin.java:259)
at com.sun.javafx.scene.control.skin.ChoiceBoxSkin$$Lambda$227/143169386.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1364)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$324(ContextMenuContent.java:1317)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$231/1415399751.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
それ以外の場合は、ちょうど – Vampire
zweitehu.FXMLDocumentController.lambda $ setListeners $ -1(FXMLDocumentController.javaを推測され、あなたの質問にソースコードを追加してください、ライン248は、面白いコンテンツを持っているようです。 –
@BjörnKautlerコードを追加しました – AlenB