2016-04-12 12 views
1

これは私の最初の質問ですので、何か問題がある場合は私に怒ってはいけません。私は現在、JavaFxの助けを借りてJava用のゲームのようにアイドル状態をプログラミングしています。 ここのコードはeclipse marsとJavaFxで書かれています。コードは正しいですが、私はプログラムを起動したい場合には、エラーメッセージのこのブロックを投げることを私に示して日食:JavaFxプログラムはjava.lang.reflect.InvocationTargetExceptionをスローします

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

私はここで誰もがヒントや、この問題の解決策を持っている:)非常だろう願っていますあなたから素晴らしい。

ベスト挨拶、 マイク

+4

完全なスタックトレースを含めるように質問してください。また、[例外を投げている行](http://stackoverflow.com/questions/3988788)を特定できる場合にも役立ちます。 –

+0

すべてのパッケージが正しくインポートされています。 –

答えて

1

私はあなたの問題は、入力は、あなたのImageオブジェクトを作成するために使用するストリームに関係していと思う:

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

私はEclipseでアプリケーションを起動すると、私が手InvocationTargetExceptionも同様です。 PNGファイルが見つからないため、NullPointerExceptionが原因です。

Imageオブジェクトを作成せずにアプリケーションを起動して、例外が発生するかどうかを確認してください。

+0

フィードバックをいただきありがとうございます。私はそれをチェックして、pngなしで動作します...これはpngの標準ファイルパスですか?パッケージのルートフォルダまたはsrcフォルダ? –

+0

相対パスまたは絶対パスを使用できます。相対パス(「not.png」など)を使用すると、「not.png」ファイルは、クラスファイルがあるフォルダを基準として検索されます。詳細はこちら[here](https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream)を参照してください。 – Uwe