手动设置控制器后出现javafx.fxml.LoadException

时间:2018-06-20 21:38:50

标签: java javafx

我对JavaFX和stackoverflow也很陌生,所以如果我说错了,请纠正我。

我的应用程序将类似于网上商店。 登录后,登录窗口关闭,主窗口打开。它包含一个导航到购物车,聊天,产品目录等的工具栏,以及一个空窗格,该窗格根据单击的工具栏按钮加载并显示内容。产品目录是起始页。

我必须使用外观不同的productcatalog窗格的不同对象类型(因为这是大学的一项工作),具体取决于我是以买家,卖家还是管理员身份登录。

我使用的是我从MainPaneController传递给KatalogPaneController的构造方法的activeAccountDTO对象,因此initialize()方法可以使用'activeAccountDTO来检查是否应加载管理,采购或卖方目录管理员实例”。要调用此构造方法,我必须从fxml文件中删除fx:controller并手动进行设置,以避免在initialize()内出现nullpointerException。

但是,如果我这样做,我会收到以下异常:

javafx.fxml.LoadException: 
/C:/Users/Andi/IdeaProjects/agoraB2B/out/production/agoraB2B/GUI/katalogPane.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at GUI.MainPaneController.produktKatalogAnzeigen(MainPaneController.java:45)
at GUI.MainPaneController.initialize(MainPaneController.java:33)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at GUI.RegisterWindowController.createAccount(RegisterWindowController.java:119)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8863)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
    at GUI.KatalogPaneController.initialize(KatalogPaneController.java:37)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    ... 66 more

当我像其他页面那样简单地使用load()函数时,并没有手动设置Controller,这没有发生。 MainPaneController类的唯一重要的方法(关于我的问题)是produktKatalogAnzeigen()。

所以,这是我的代码,希望您能提供帮助! :)

package GUI;

import Accountsystem.AccountDTO;
import Accountsystem.Admin;
import Accountsystem.Verkaeufer;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class KatalogPaneController implements Initializable {

    @FXML
    private Button software, elektronik, buero, rhb;

    private AccountDTO activeAccountDTO;
    private MainPaneController motherPaneController;
    private FXMLLoader loader;

    @FXML
    private Pane katalogContent;


    public KatalogPaneController(AccountDTO activeAccountDTO) { // Um die Variable setzen zuu können, bevor die initialize-Methode aufgerufen wird (Der Controller ist in der FXML nicht definiert, sondern manuell!)
        setActiveAccountDTO(activeAccountDTO);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        if(activeAccountDTO.getBenutzer() instanceof Admin) {
            adminsichtAnzeigen();
        } else if (activeAccountDTO.getBenutzer() instanceof Verkaeufer) {
            verkaeufersichtAnzeigen();
        } else {
            kaeufersichtAnzeigen();
        }
        // TODO: (evtl. nicht an dieser stelle): Buttons der Tabelle hinzufügen für Adminsicht: https://stackoverflow.com/questions/29489366/how-to-add-button-in-javafx-table-view
    }

    public void adminsichtAnzeigen() {
        Parent content = null;
        try {
            loader = new FXMLLoader(getClass().getResource("katalogContentAdminsicht.fxml"));
            content = loader.load();
            ((KatalogContentAdminsichtController)loader.getController()).setActiveAccountDTO(activeAccountDTO); // AccountDTO-Objekt an Controller übergeben (geht vlt auch einfacher, von innerhalb des Controllers getten?)
            ((KatalogContentAdminsichtController)loader.getController()).setMotherPaneController(this);
            katalogContent.getChildren().setAll(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void verkaeufersichtAnzeigen() {
        Parent content = null;
        try {
            loader = new FXMLLoader(getClass().getResource("katalogContentVerkaeufersicht.fxml"));
            content = loader.load();
            ((KatalogContentVerkaeufersichtController)loader.getController()).setActiveAccountDTO(activeAccountDTO); // AccountDTO-Objekt an Controller übergeben (geht vlt auch einfacher, von innerhalb des Controllers getten?)
            ((KatalogContentVerkaeufersichtController)loader.getController()).setMotherPaneController(this);
            katalogContent.getChildren().setAll(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void kaeufersichtAnzeigen() {
        Parent content = null;
        try {
            loader = new FXMLLoader(getClass().getResource("katalogContentKaeufersicht.fxml"));
            content = loader.load();
            ((KatalogContentKaeufersichtController)loader.getController()).setActiveAccountDTO(activeAccountDTO); // AccountDTO-Objekt an Controller übergeben (geht vlt auch einfacher, von innerhalb des Controllers getten?)
            ((KatalogContentKaeufersichtController)loader.getController()).setMotherPaneController(this);
            katalogContent.getChildren().setAll(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setActiveAccountDTO(AccountDTO activeAccountDTO) {
        this.activeAccountDTO = activeAccountDTO;
    }

    public void setMotherPaneController(MainPaneController mainPaneController) {
        this.motherPaneController = mainPaneController;
    }
}

----

package GUI;

import Accountsystem.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainPaneController implements Initializable {

    @FXML
    BorderPane rootPane;
    @FXML
    Pane contentPane;

    FXMLLoader loader; // Um auf die Controller-Objekte der eingelesenen Panes zugreifen zu können
    private AccountDTO activeAccountDTO;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
//        Admin testAdmin = new Admin("Test", "AccountDTO", "Test", "0123456789", "testemail@test.de" ,
//                new Adresse("Teststreet", "123", "45279", "Essen", "DE", AdressTyp.KontaktAdresse));
//        activeAccountDTO = new AccountDTO();
        try {
            produktKatalogAnzeigen();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void produktKatalogAnzeigen() throws IOException {
//        Parent content = FXMLLoader.load(getClass().getResource("katalogPane.fxml"));
        loader = new FXMLLoader();
        loader.setController(new KatalogPaneController(activeAccountDTO)); // Muss manuell gesetzt werden, um activeAccountDTO zu übergeben. Controller ist in der FXML Datei bewusst nicht gesetzt.
        loader.setLocation(getClass().getResource("katalogPane.fxml"));
        Parent content = loader.load();
//        ((KatalogPaneController)loader.getController()).setActiveAccountDTO(activeAccountDTO); // AccountDTO-Objekt an Controller übergeben (geht vlt auch einfacher, von innerhalb des Controllers getten?)
        ((KatalogPaneController)loader.getController()).setMotherPaneController(this);
//        Parent content = loader.load();
        contentPane.getChildren().setAll(content);
//        System.out.println(contentPane);
    }

    @FXML
    public void postfachAnzeigen() throws IOException {
//        Parent content = FXMLLoader.load(getClass().getResource("postfachPane.fxml"));
        loader = new FXMLLoader(getClass().getResource("postfachPane.fxml"));
        Parent content = loader.load();
        contentPane.getChildren().setAll(content);
    }

    @FXML
    public void accountAnzeigen() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

//        Parent content = FXMLLoader.load(getClass().getResource("accountPane.fxml"));
        loader = new FXMLLoader(getClass().getResource("accountPane.fxml"));
        Parent content = loader.load(); // Immer erst load aufrufen, bevor man auf den Controller zugreift !!
//        System.out.println(loader.getController().toString());
//        System.out.println(loader.toString());
        ((AccountPaneController)loader.getController()).setActiveAccountDTO(activeAccountDTO); // AccountDTO-Objekt an Controller übergeben (geht vlt auch einfacher, von innerhalb des Controllers getten?)
        ((AccountPaneController)loader.getController()).setMotherPaneController(this);
        contentPane.getChildren().setAll(content);
    }

    @FXML
    public void einkaufswagenAnzeigen() throws IOException { // TODO BUGGY
//        Parent content = FXMLLoader.load(getClass().getResource("einkaufswagenPane.fxml"));
        loader = new FXMLLoader(getClass().getResource("einkaufswagenPane.fxml"));

            Parent content = loader.load();
            contentPane.getChildren().setAll(content);

        EinkaufswagenPaneController ekwController = loader.getController(); // ekwController wird zugreifbar
        ekwController.setActiveAccountDTO(activeAccountDTO); // ekwController kriegt Zugriff auf den Einkaufswagen im Accountsystem
    }

    @FXML
    public void logout() throws IOException {

        activeAccountDTO = null; // vorher alles speichern! Und auch bei window close!!

//        Parent root = FXMLLoader.load(getClass().getResource("login_window.fxml"));
        loader = new FXMLLoader(getClass().getResource("login_window.fxml"));
        Parent root = loader.load();
        rootPane.getChildren().setAll(root);
        rootPane.getScene().getWindow().setHeight(137 + 40);
        rootPane.getScene().getWindow().setWidth(311 + 16);
    }

    public AccountDTO getActiveAccountDTO() {
        return activeAccountDTO;
    }

    public void setActiveAccountDTO(AccountDTO activeAccountDTO) {
        this.activeAccountDTO = activeAccountDTO;
    }
}

0 个答案:

没有答案