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

时间:2016-10-05 19:01:51

标签: javafx

我已经在src级别的resources文件夹中保存了imgserver.png,但我仍然得到了这个错误。任何人都告诉我确切的解决方案。

   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)

2 个答案:

答案 0 :(得分:0)

尝试:

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

并在src中移动resources文件夹。

我可能知道您正在使用的IDE吗?

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

}