当我按下按钮loadMap时,我应该能够选择一个文件(图像),并且应该将其上传到我所拥有的边框的中央,但是一旦选择文件,我就会不断收到错误消息和NullPointerException。
我尝试将其作为内部类作为处理程序,但存在相同的问题。这是下面的代码:
public class Karta extends Application {
final int LIST_CELL_HEIGHT = 26;
TextField textfield;
RadioButton b1;
RadioButton b2;
Label label = new Label("Categories");
ListView<String> lView;
Button hideC;
Button New;
Button search;
Button hide;
Button remove;
Button coordinate;
FileChooser fileChooser;
File file;
ImageView imageView;
ObservableList<String> items = FXCollections.observableArrayList("Bus", "Train", "Underground");
ListView<String> list = new ListView<>(items);
MenuBar fileMenu;
Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
BorderPane root = new BorderPane();
New = new Button("New");
b1 = new RadioButton("Named");
b2 = new RadioButton("Described");
ToggleGroup group = new ToggleGroup();
b2.setToggleGroup(group);
b1.setToggleGroup(group);
VBox vInTopH = new VBox(5);
vInTopH.getChildren().addAll(b1, b2);
textfield = new TextField("Search..");
search = new Button("Search");
hide = new Button("Hide");
remove = new Button("Remove");
coordinate = new Button("Coordinate");
HBox topHbox = new HBox(5);
topHbox.getChildren().addAll(New, vInTopH, textfield, search, hide, remove, coordinate);
topHbox.setAlignment(Pos.CENTER);
VBox topVbox = new VBox(10);
fileMenu = new MenuBar();
Menu Amenu = new Menu("File");
fileMenu.getMenus().add(Amenu);
MenuItem loadMap = new MenuItem("Load Map");
MenuItem loadPlaces = new MenuItem("Load Places");
MenuItem exit = new MenuItem("Exit");
MenuItem save = new MenuItem("Save");
Amenu.getItems().addAll(loadMap, loadPlaces, exit, save);
topVbox.getChildren().add(fileMenu);
topVbox.getChildren().add(topHbox);
hideC = new Button("Hide Category");
lView = new ListView<>();
lView.setItems(items);
lView.setPrefHeight(lView.getItems().size() * LIST_CELL_HEIGHT);
VBox vBox = new VBox(10);
vBox.setAlignment(Pos.BASELINE_CENTER);
vBox.setAlignment(Pos.CENTER);
vBox.getChildren().add(label);
vBox.getChildren().addAll(lView, hideC);
root.setCenter(imageView);
root.setRight(vBox);
root.setTop(topVbox);
loadMap.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
String filename = file.getAbsolutePath();
Image image = new Image("file:"+filename);
imageView.setImage(image);
primaryStage.sizeToScene();
}
});
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
结果应该是设置时所选文件填充了边框的中心。
下面是stacktrace:
objc[35637]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fffa822fc90) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x125538cd8). One of the two will be used. Which one is undefined.
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Karta.lambda$0(Karta.java:121)
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:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
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:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
答案 0 :(得分:0)
检查代码的第121行,您正在尝试访问null变量的成员:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at Karta.lambda$0(Karta.java:121)
...
很有可能是imageView
设置为null
而不是预期的有效对象引用。这将是抛出异常的行参考:
imageView.setImage(image);
可能需要更多细节才能进行更深入的分析。