2016-10-05 4 views
1

imgserver.pngをsrcレベルのresourcesフォルダに保存しましたが、このエラーが発生します。私はsrcレベルでリソースフォルダに画像を保存していますが、アプリケーション開始メソッドで例外が発生しました

package javafxapplication1; 

    import com.sun.javaws.Main; 
    import java.io.InputStream; 
    import javafx.application.Application; 
    import javafx.geometry.Insets; 
    import javafx.geometry.Pos; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.image.Image; 
    import javafx.scene.image.ImageView; 
    import javafx.scene.layout.BorderPane; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.scene.paint.Color; 
    import javafx.scene.text.Font; 
    import javafx.scene.text.FontPosture; 
    import javafx.scene.text.FontSmoothingType; 
    import javafx.scene.text.Text; 
    import javafx.stage.Stage; 

    public class guest extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
    primaryStage.setTitle("E-magazine"); 
    /*GridPane grid=new GridPane(); 
    grid.setAlignment(Pos.CENTER); 

    grid.setVisible(true);*/ 
    BorderPane border=new BorderPane(); 
    HBox hbox=addHBox(); 
    border.setTop(hbox); 
    border.setLeft(addVBox()); 
    //addStackPane(hbox); 
    //border.setCenter(addGridPane()); 
    //border.setRight(addFlowPane()); 
    Scene scene=new Scene(border,1000,500); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    } 
    public HBox addHBox() 
    { 
        HBox hbox=new HBox(); 
        hbox.setPadding(new Insets(15,12,15,12)); 
       hbox.setSpacing(10); 
      hbox.setStyle("-fx-background-color:#336699;"); 
     Text title=new Text("E-MAGAZINE!"); 
       title.setFont(Font.font("VERDANA",FontPosture.ITALIC,30)); 
      title.setFill(Color.WHITE); 
      title.setFontSmoothingType(FontSmoothingType.LCD); 
      hbox.setAlignment(Pos.CENTER); 
    Image image=new Image(this.getClass().getResourceAsStream("/imgserver.png")); 
     ImageView iv1=new ImageView(image); 

     iv1.setImage(image); 
     hbox.getChildren().add(title); 
     hbox.getChildren().add(iv1); 
     return hbox; 
     } 
     public VBox addVBox() 
     { 
     VBox vbox=new VBox(); 
     vbox.setPadding(new Insets(10)); 
     vbox.setSpacing(100); 
     Button buttonArticle=new Button("ARTICLES"); 
     buttonArticle.setPrefSize(100,50); 
     Button buttonNews=new Button("NEWS"); 
     buttonNews.setPrefSize(100,50); 
     vbox.getChildren().addAll(buttonArticle,buttonNews); 
     return vbox; 
     } 

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

     } 

スタックトレース:

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: 
Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) 
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: Input stream must not be null 
    at javafx.scene.image.Image.validateInputStream(Image.java:1001) 
    at javafx.scene.image.Image.<init>(Image.java:624) 
    at javafxapplication1.guest.addHBox(guest.java:55) 
    at javafxapplication1.guest.start(guest.java:32) 
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219) 
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182) 
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67) 
    ... 1 more 
C:\Users\hp pc\Documents\NetBeansProjects\JavaFXApplication1\nbproject\build-impl.xml:1054: The following error occurred while executing this line: 
C:\Users\hp pc\Documents\NetBeansProjects\JavaFXApplication1\nbproject\build-impl.xml:807: Java returned: 1 
BUILD FAILED (total time: 1 second) 
+0

'getResourceAs'を使用せず、' getResource'を使用してください。 –

+0

ビルドフォルダの内容を確認できますか? –

+0

Image(URL)に適切なコンストラクタが見つからないというエラーが表示されます@MichaelPickett –

答えて

0

試してみてください。

Image image = new Image(getClass().getResourceAsStream("/resources/imgserver.png")) 

とSRC内のリソースフォルダを移動します。

あなたが使用しているIDEを知っているかもしれませんか?

+0

私はide 8.1を使用していますが、エラーはまだ存在します –

0

用途:

Image image = new Image(ClassLoader.getSystemResourceAsStream("imgserver.png")); 

プロジェクトレイアウト:

This is the project layout

アプリケーションウィンドウ: enter image description here

全アプリコード:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontSmoothingType; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 


public class guest extends Application 
{ 

    @Override 
    public void start(Stage primaryStage) 
    { 
     primaryStage.setTitle("E-magazine"); 
     /* 
     * GridPane grid=new GridPane(); grid.setAlignment(Pos.CENTER); 
     * 
     * grid.setVisible(true); 
     */ 
     BorderPane border = new BorderPane(); 
     HBox hbox = addHBox(); 
     border.setTop(hbox); 
     border.setLeft(addVBox()); 
     // addStackPane(hbox); 
     // border.setCenter(addGridPane()); 
     // border.setRight(addFlowPane()); 
     Scene scene = new Scene(border , 1000 , 500); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public HBox addHBox() 
    { 
     HBox hbox = new HBox(); 
     hbox.setPadding(new Insets(15 , 12 , 15 , 12)); 
     hbox.setSpacing(10); 
     hbox.setStyle("-fx-background-color:#336699;"); 
     Text title = new Text("E-MAGAZINE!"); 
     title.setFont(Font.font("VERDANA" , FontPosture.ITALIC , 30)); 
     title.setFill(Color.WHITE); 
     title.setFontSmoothingType(FontSmoothingType.LCD); 
     hbox.setAlignment(Pos.CENTER); 
     Image image = new Image(ClassLoader.getSystemResourceAsStream("imgserver.png")); 
     ImageView iv1 = new ImageView(image); 

     iv1.setImage(image); 
     hbox.getChildren().add(title); 
     hbox.getChildren().add(iv1); 
     return hbox; 
    } 

    public VBox addVBox() 
    { 
     VBox vbox = new VBox(); 
     vbox.setPadding(new Insets(10)); 
     vbox.setSpacing(100); 
     Button buttonArticle = new Button("ARTICLES"); 
     buttonArticle.setPrefSize(100 , 50); 
     Button buttonNews = new Button("NEWS"); 
     buttonNews.setPrefSize(100 , 50); 
     vbox.getChildren().addAll(buttonArticle , buttonNews); 
     return vbox; 
    } 

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

} 
+0

あなたはどのIDEを使用しているか教えていただけますか? –

+0

Eclipse Neon + Gradle buildshipプラグイン – Stas

関連する問題