0

When I press the button loadMap I should be able to choose a file (image) and it should be uploaded to the center of the borderpane which I have, but I keep getting error and NullPointerException as soon as I choose a file.

I have tried having it as a inner class as a handler but its the same problem. Here is the following code :

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();

    }



}

The results should be that the chosen file fills the center of the borderpane when set.

Below, the 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)

1 Answers1

0

Check line 121 of your code, you are trying to access members of a null variable:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at Karta.lambda$0(Karta.java:121)...

Most likely it is the imageView that is set to null and not to a valid object reference as expected. This would be the line reference throwing the exception:

imageView.setImage(image);

More details may be needed to do a more in depth analyis.

Marco R.
  • 2,667
  • 15
  • 33
  • yes you are correct it is the ''image.setImage(image)" line that gets the wrong. I have updated with the full code, do you see anything that makes this happen? i can only assume i am giving the "Image image" a wrong type of value? – Roni Berri May 16 '19 at 08:48
  • 2
    No, you have `imageView` declared (and assigned `null` by default); but you haven't assigned any object to it, missing something like `ImageView imageView = new ImageView();` – Marco R. May 16 '19 at 09:10
  • you're smart man, thank you it worked now! – Roni Berri May 16 '19 at 09:41