从FXML加载控制器时出现IllegalArgumentException [英] IllegalArgumentException when loading controller from FXML

查看:42
本文介绍了从FXML加载控制器时出现IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在为学校项目编码此聊天客户端,现在我需要使用JavaFX来实现GUI.我已经在Scene Builder中构建了外观简洁的GUI.现在,无论何时客户端收到一条消息,它都应该在控制器中调用一个方法,以将接收到的字符串附加到TextField.

So I'm coding this chat-client for a school project, and now I need to implement a GUI, using JavaFX. I've built a neat looking GUI in Scene Builder. Whenever the client recieves a message it should now call a method in the controller to append the recived string to a TextField.

但是,当我尝试从FXML文件加载控制器时,出现此错误:

However, when I try to load the controller from the FXML-file I get this error:

null/../../../../img/jim.png
javafx.fxml.LoadException: 
unknown path:16

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.example.main.GUI.start(GUI.java:46)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1100)
at javafx.scene.image.Image.<init>(Image.java:681)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 11 moreCaused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1092)
... 17 more

这是我获取控制器并在其中调用方法的代码:

Here's my code for getting the controller and calling a method in it:

    try {
        FXMLLoader loader = new FXMLLoader();
        Pane p = loader.load(getClass().getResource("ChatForm.fxml").openStream());
        ChatForm chatForm = (ChatForm) loader.getController();
        chatForm.test();
    } catch (IOException e) {
        e.printStackTrace();
    }

最后,我的FXML文件:

Finally, my FXML file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="410.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.main.ChatForm">
   <children>
      <VBox prefHeight="410.0" prefWidth="300.0">
         <children>
            <ImageView fitHeight="135.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../../img/jim.png" />
               </image>
            </ImageView>
            <TextArea fx:id="chatTextArea" editable="false" focusTraversable="false" prefHeight="232.0" prefWidth="300.0" wrapText="true" />
            <TextField fx:id="messageTextField" />
            <Label text="Skriv &quot;.disconnect&quot; för att logga ut">
               <font>
                  <Font size="10.0" />
               </font>
            </Label>
     </children>
  </VBox>

显然,加载徽标图像时出现了奇怪的现象.有什么想法吗?

Clearly something strange is up when loading the logo-image. Any ideas?

新年快乐!

推荐答案

指的是相对于当前FXMLLoader的location属性的路径.

The syntax url="@..." in your FXML file refers to a path relative to the current FXMLLoader's location property.

但是,当加载FXML文件时,您不是为FXMLLoader提供位置,而是向其提供输入流.因此,位置为null并且(如堆栈跟踪所示),您的路径被解释为

However, when you load the FXML file, you are not providing the FXMLLoader with a location, but an input stream. Thus the location is null and (as seen in the stack trace), your path gets interpreted as


null/../../../../img/jim.png

要解决此问题,请提供FXMLLoader的位置,而不是输入流:

To fix this, provide the location to the FXMLLoader, instead of the input stream:

try {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatForm.fxml"));
    Pane p = loader.load();
    ChatForm chatForm = (ChatForm) loader.getController();
    chatForm.test();
} catch (IOException e) {
    e.printStackTrace();
}

这将解决紧迫的问题,前提是该路径实际上是正确的.我认为您还有其他本质上不相关的问题,因为您似乎从未对加载的场景图做任何事情(即,您从未显示窗格p).

This will fix the immediate problem, assuming the path is actually correct. I think you still have other, essentially unrelated, problems, as you never seem to do anything with the scene graph you load (i.e. you never display the pane p).

这篇关于从FXML加载控制器时出现IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆