当前位置: 首页 > 知识库问答 >
问题:

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

计和顺
2023-03-14

有两个文件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 

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

} 

<?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> 

共有1个答案

怀德馨
2023-03-14

您可以定义一个sepreate控制器,并可以轻松地管理事情。在FXMLLodar的场景中,您正在调用同一个类的另一个实例,因此这就是该场景提供NPE的方式。

 类似资料:
  • 问题内容: 我试图在JavaFX中获得TableView来动态显示内容。 当我运行程序时,出现此错误: 我的控制器名为“ UserInterfaceController.java”,它与FXML文件位于同一软件包下,我也已将该软件包导入了FXML中。为什么找不到控制器? FXML文件: 控制器: 主类: 问题答案: 使用FXML的全限定名来引用您的控制器: 后续问题的答案 对以下问题的回答解释了为

  • 我试图在JavaFX中获得一个TableView来动态显示内容。 当我运行程序时,出现以下错误: 我的控制器名为“UserInterfaceController.java”,它与FXML文件在同一个包下,我也在FXML中导入了包。为什么找不到控制器? FXML文件: 控制器: 主要类:

  • 我有以下代码: 在fxml文件中有一个对控制器类的引用。如何获取控制器对象? fxml:

  • 问题内容: 因此,我使用此链接来设置进度条,并且在运行此代码时效果很好。 但是,使用我拥有的设置,我似乎无法获得progressbar进行实际更新… progressBar.getProgress()实际上获得了正确的进度,但是UI并未更新。这是到目前为止的代码: Main.java ProgressBarController.java ProgressBarLayout.fxml 问题答案: 在