Javafx 应用程序启动方法中出现异常,即使我已将图像保存在src级别的资源文件夹中

Javafx 应用程序启动方法中出现异常,即使我已将图像保存在src级别的资源文件夹中,javafx,Javafx,我已经将imgserver.png保存在src级别的参考资料文件夹中,但仍然出现此错误。有人能告诉我确切的解决方案吗 package javafxapplication1; import com.sun.javaws.Main; import java.io.InputStream; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geom

我已经将imgserver.png保存在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);
      }

      }
堆栈跟踪:

应用程序启动方法中出现异常 线程“main”java.lang.RuntimeException中的异常: 应用程序启动方法中的异常 位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:403) 访问com.sun.javafx.application.launchempl.access$000(launchempl.java:47) 位于com.sun.javafx.application.launchempl$1.run(launchempl.java:115) 运行(Thread.java:745) 原因:java.lang.NullPointerException:输入流不能为null 位于javafx.scene.image.image.validateInputStream(image.java:1001) 在javafx.scene.image.image.(image.java:624) 在javafxapplication1.guest.addHBox(guest.java:55) 在javafxapplication1.guest.start(guest.java:32) 位于com.sun.javafx.application.launchempl$5.run(launchempl.java:319) 位于com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219) 位于com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182) 位于com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179) 位于java.security.AccessController.doPrivileged(本机方法) 位于com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179) 位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:76) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17) 位于com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67) ... 还有一个 C:\Users\hp pc\Documents\NetBeansProjects\JavaFXApplication1\nbproject\build impl.xml:1054:执行此行时发生以下错误: C:\Users\hp pc\Documents\NetBeansProjects\JavaFXApplication1\nbproject\build impl.xml:807:Java返回:1 生成失败(总时间:1秒) 试试:

Image image = new Image(getClass().getResourceAsStream("/resources/imgserver.png"))
并将resources文件夹移动到src中

我可以知道您使用的IDE吗?

试试:

并将resources文件夹移动到src中

我可以知道您使用的IDE吗?

使用:

Image image = new Image( ClassLoader.getSystemResourceAsStream( "imgserver.png" ) );
项目布局:

应用程序窗口:

完整应用程序代码:

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

}
使用:

项目布局:

应用程序窗口:

完整应用程序代码:

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

}

使用
getResource
而不是
getResourceAsStream
。您可以检查生成文件夹的内容吗?它显示一个错误,表示没有为Image(URL)@michaelpicket找到合适的构造函数,我应该在生成文件夹中搜索什么?请指定@James_dt FXML文件。。。具体地说,它是否存在,如果存在,它在哪个子文件夹(如果有)中使用
getResource
而不是
getresourceastream
。您可以检查生成文件夹的内容吗?它显示一个错误,表示找不到适合图像(URL)的构造函数@michaelpicket我应该在内置文件夹中搜索什么?请指定@James\u dt FXML文件。。。具体地说,它在那里吗?如果是的话,它在哪个子文件夹中(如果有)你能告诉我你在使用哪个IDE吗?Eclipse Neon+Gradle buildship插件你能告诉我你在使用哪个IDE吗?Eclipse Neon+Gradle buildship插件