无法在 javafx 中链接 fxml 和控制器文件变量 [重复]

Posted

技术标签:

【中文标题】无法在 javafx 中链接 fxml 和控制器文件变量 [重复]【英文标题】:Do not able to link fxml and controller file variable in javafx [duplicate] 【发布时间】:2019-10-15 19:33:44 【问题描述】:

有两个文件 user.fxml 和 userListPaneFx.java。在这种情况下,错误来自 userListView 变量。我写得对,但错误来自空指针异常。所以我能做些什么来解决这个问题。两个变量的法术之间没有匹配失败。

错误说明

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    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.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 48 more
Caused by: java.lang.NullPointerException
    at com.automated.chat_app.UserListPaneFx.<init>(UserListPaneFx.java:38)
    at com.automated.chat_app.LoginController.Login(LoginController.java:28)
    ... 58 more 

userListPaneFx.java的代码

 package com.automated.chat_app;

import java.io.IOException;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.collections.*;
import javafx.fxml.*;

public class UserListPaneFx implements UserStatusListner 

    private ChatClient client;
    @FXML 
    public javafx.scene.control.ListView userListView;

    public UserListPaneFx(final ChatClient client) throws IOException
    
        this.client = client;
        this.client.online_user(this);
        ObservableList<String> userList = FXCollections.observableArrayList();
        System.out.println("Hello World");
         Parent root = FXMLLoader.load(getClass().getResource("user.fxml"));
         Stage primaryStage = new Stage();
          primaryStage.setTitle("UserList");
          primaryStage.setScene(new Scene(root,315,557));
          primaryStage.show();
          userList.addAll("Hello World","HiiMohan");
          System.out.println(userListView);
          userListView.getItems().addAll(userList);
    

    public UserListPaneFx()
    

    



    @Override
    public void online(String login) 
        userListView.getItems().add(login);
    


    @Override
    public void offline(String login) 
        userListView.getItems().remove(login);
    

 

编码 user.fxml 文件

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="485.0" prefWidth="279.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.automated.chat_app.UserListPaneFx">
   <children>
      <ListView id="userListView" fx:id="userListView" prefHeight="485.0" prefWidth="279.0" />
   </children>
</AnchorPane> 

【问题讨论】:

如果有 NPE,知道什么是 null 而你不希望是 null 就没什么用了。您是否调试过代码(特别是构造函数)?或者否则第 38 行在哪里(因为它被指出是抛出 NPE)?或者也许你应该看看你的 Login 类,因为那个正在调用构造函数? 另外,您当前的设置方式意味着FXMLLoader 将创建一个新的、独立的UserListPaneFx 类实例。这意味着与 UI 关联的控制器不会初始化其 client 字段。见Passing Parameters JavaFX FXML 和JavaFX : Pass parameters while instantiating controller class。 【参考方案1】:

您可以定义一个单独的控制器并且可以轻松地管理事物。在您的场景中,您正在从 FXMLLodar 调用同一类的另一个实例,因此这种场景是给 NPE 的。

【讨论】:

以上是关于无法在 javafx 中链接 fxml 和控制器文件变量 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

JavaFX 在没有控制器的 fxml 中包含 fxml

无法在 JavaFX 中加载 FXML 文件

设置root的JavaFx FXML加载文件问题

在控制器中编辑文本区域

JavaFX 8 - 每个选项卡带有单独的 FXML 和控制器的选项卡和选项卡

如何使用不同的 fxml 文件创建多个 javafx 控制器?