【问题标题】:Understanding Javafx runtime errors [duplicate]了解 Javafx 运行时错误 [重复]
【发布时间】:2018-12-26 02:47:16
【问题描述】:

我使用 JavaFX 为推箱子游戏编写了代码,但没有完成, 如您所见,这里有三个部分,首先是初始化,还有两个函数。 当我运行程序时,它会出现一堆错误,这是第一次 让我得到这样的java错误,我认为这不是语法错误,拜托 如果您知道如何解决这个问题,请给我您的反馈,谢谢。

public class Main extends Application {
    private double width = 32*20;
    private double height = 32*20;
    private int[][] grid;
    private int[][] targets;
    private Image image = new Image("man.png");
    private ImageView man = new ImageView(image);
    private ArrayList<ImageView> boxList = new ArrayList<ImageView>();
    private Pane warehouse;

    public void warehouseInitialize(){
        warehouse.getChildren().clear();

        int[][] tempGrid = {     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,2,0,0,1,1,1,1,1,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
        this.grid = tempGrid;
        int [][] tempTargets = {{3,3},{18,18},{16,5}};
        this.targets = tempTargets;
        warehouse.setStyle("-fx-background-color:green");

        for(int row=0;row<20;row++){
            for(int col=0;col<20;col++){
                if(grid[row][col] == 1){
                    ImageView stone = new ImageView(new Image("stone.png"));
                    stone.setX(row*32);
                    stone.setY(col*32);
                    warehouse.getChildren().add(stone);
                }else if(grid[row][col] == 2){
                    ImageView box = new ImageView(new Image("box.png"));
                    box.setX(row*32);
                    box.setY(col*32);
                    warehouse.getChildren().add(box);
                    boxList.add(box);
                }
            }
        }

        man.setX(10*32);
        man.setY(10*32);
        warehouse.getChildren().add(man);

        for(int i=0;i<targets.length;i++){
            Circle circle = new Circle();
            circle.setRadius(16);
            circle.setCenterX(tempTargets[i][i] * 32);
            circle.setCenterY(tempTargets[i][i+1]*32);
            circle.setStyle("-fx-stroke:red; -fx-fill:blue;");
            warehouse.getChildren().add(circle);
        }
    }
    @Override
    public void start(Stage primaryStage)throws Exception{
        MenuBar menuBar = new MenuBar();
        Menu menu1 = new Menu("Tool");
        menuBar.getMenus().add(menu1);
        MenuItem menu12 = new MenuItem("Reset");
        menu1.getItems().add(menu12);
        menu12.setOnAction(e->{
            warehouseInitialize();
        });
        primaryStage.setTitle("Sokoban");
        primaryStage.setWidth(width);
        primaryStage.setHeight(height + menuBar.getHeight() + 64);
        primaryStage.setResizable(false);

        VBox vBox = new VBox(menuBar,warehouse);

        Scene scene = new Scene(vBox,800,800);
        scene.setOnKeyPressed(e->{
            switch(e.getCode()){
                /*case UP:
                    //goUp();
                    break;
                case DOWN:
                    //goDown();
                    break;
                case RIGHT:
                    //goRight();
                    break;*/
                case LEFT:
                    goLeft();
                    break;
            }
        });
    }
  public void goLeft(){
        int left1 = (int)((man.getX() - 32) / 32);
        int left2 = (int)((man.getX() - 64) / 32);
        int y = (int)(man.getY() / 32);
        if(grid[y][left1] == 0){
            man.setX(left1*32);
            man.setY(y*32);
        }else if(grid[y][left1] == 2 && grid[y][left2]==0){
            man.setX(left1*32);
            man.setY(y*32);
            for(ImageView box :boxList){
                int x_b = (int)(box.getX() / 32);
                int y_b = (int)(box.getY() / 32);
                if(x_b == y && y_b == left2 + 1){
                    box.setX(y*32);
                    box.setY(left2 * 32);
             }
         }
     }
  }
}

运行程序时出现以下错误:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class sample.Main
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at sample.Main.<init>(Main.java:24)
... 13 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 15 more
Exception running application sample.Main

【问题讨论】:

  • 错误也贴出来了
  • Invalid URL: Invalid URL or resource not found - 这意味着它找不到该图像文件。

标签: java javafx runtime-error illegalargumentexception


【解决方案1】:

错误是由于错误的图像路径。要测试您的代码,您可以使用网络资源:

        Image image = new Image("https://candytech.in/wp-content/uploads/2018/04/man-1.png");
        ImageView man = new ImageView(image);

man.pngMain 位于同一文件夹中时,发布的代码private Image image = new Image("man.png"); 应该可以工作。
与问题无关:warehouse 从未初始化。

【讨论】:

    【解决方案2】:

    堆栈跟踪非常详细,并提供了足够的信息进行调试。您可以看到caused by 的序列,这意味着上面的异常是由它下面的异常引起的。

    所以很可能在您指定的位置找不到您的图像。请重新检查并考虑检查有关相对路径的内容。

    【讨论】:

    • 您能否推荐正确形式的图像路径示例?
    【解决方案3】:

    错误是:

    Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    

    您的代码中的重点在这里:

    at sample.Main.<init>(Main.java:24)
    

    如果我是正确的,指的是这一行:

    private Image image = new Image("man.png");
    

    问题是找不到man.png。但是,根据this question,有两种可能的解决方法。一、try this

    private Image image = new Image("/sample/man.png");
    

    默认情况下,Image 的根目录是src 目录。

    如果失败,this might work:

    private File file = new File("C:\\Users\\Yusuf\\Desktop\\Foundation of Multimedia\\lab\\Sokoban\\src\\sample\\man.png");
    private Image image = new Image(file.toURI().toString());
    

    这里我们创建一个指向所需文件的File 对象,然后使用toURI() 获取一个URI。您可能可以使用以下方法跳过File 对象:

    private Image image = new Image("file://C/Users/Yusuf/Desktop/Foundation of Multimedia/lab/Sokoban/src/sample/man.png");
    

    但是,转义空格字符等可能存在问题,因此使用中间 File 对象可能更容易。

    【讨论】:

    • 我改成了这个,private Image image = new Image("C:\\Users\\Yusuf\\Desktop\\Foundation of Multimedia\\lab\\Sokoban\\src\\sample\ \man.png");但错误根本没有改变
    • 请提供此案例的正确路径示例
    • @user8729892 好的,我已经更新了几个可能的解决方案。
    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 2022-01-01
    • 2016-11-27
    • 2018-06-01
    相关资源
    最近更新 更多