1

This is my first question here so don´t be to mad at me if something is wrong. I am currently programming an idle like game for java with help of JavaFx. The code here under was written with eclipse mars and JavaFx. Eclipse shows to me that the code is right but when I want to start the program it throws this block of error messages:

public class Main extends Application{

private static final Color color = Color.web("#464646");
Button button3 = new Button("D");
DropShadow shadow = new DropShadow();
Label label = new Label();

public static void main(String[] args) throws Exception {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception{
    Scene scene = new Scene(new Group());
    primaryStage.setTitle("Button Sample");
    primaryStage.setWidth(300);
    primaryStage.setHeight(190);

    label.setFont(Font.font("Times New Roman", 22));
    label.setTextFill(color);

    Image imageDecline = new Image(getClass().getResourceAsStream("../not.png"));
    Image imageAccept = new Image(getClass().getResourceAsStream("../ok.png"));

    VBox vbox = new VBox();
    vbox.setLayoutX(20);
    vbox.setLayoutY(20);
    HBox hbox1 = new HBox();
    HBox hbox2 = new HBox();

    Button button1 = new Button("Accept");
    button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    Button button2 = new Button("Accept");
    button2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    button3.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Declined");
        }
    });

    button3.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            button3.setEffect(shadow);
        }
    });

    button3.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            button3.setEffect(null);
        }
    });

    hbox1.getChildren().add(button2);
    hbox1.getChildren().add(button3);
    hbox1.getChildren().add(label);
    hbox1.setSpacing(10);
    hbox1.setAlignment(Pos.BOTTOM_CENTER);

    Button button4 = new Button();
    button4.setGraphic(new ImageView(imageAccept));
    button4.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    Button button5 = new Button();
    button5.setGraphic(new ImageView(imageDecline));
    button5.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Declined");
        }
    });

    hbox2.getChildren().add(button4);
    hbox2.getChildren().add(button5);
    hbox2.setSpacing(25);

    vbox.getChildren().add(button1);
    vbox.getChildren().add(hbox1);
    vbox.getChildren().add(hbox2);
    vbox.setSpacing(10);
    ((Group) scene.getRoot()).getChildren().add(vbox);

    primaryStage.setScene(scene);
    primaryStage.show();
}}

I hope anyone here has a hint or a solution for this problem :) Would be very nice from you.

Best Greetings, Mike

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
  • 4
    Please [edit] your question to include the full stack trace. It is also helpful if you can identify [which line is throwing the exception](http://stackoverflow.com/questions/3988788). – James_D Apr 12 '16 at 16:26
  • All packages are correctly imported btw. – Mike Sluzalek Apr 12 '16 at 16:27

1 Answers1

1

I think your problem has to do with the input streams you use for creating your Image objects:

... = new Image(getClass().getResourceAsStream("../not.png"));

When I start your application in Eclipse, I get an InvocationTargetException as well. It is caused by a NullPointerException because the PNG files cannot be found.

Try to start your application without creating the Image objects and see if you still get any exceptions.

Uwe
  • 844
  • 8
  • 13
  • Thanks for the feedback I checked it and it works without the pngs ... which is the standard file path for png´s? Root C oder src folder of the package? – Mike Sluzalek Apr 13 '16 at 11:49
  • You can use relative or absolute paths. If you use a relative path (e.g. "not.png"), then the "not.png" file will be looked for relative to the folder in which your class file is located. More info can be found [here](https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream). – Uwe Apr 14 '16 at 18:43