FXML AnchorPane为空

时间:2019-02-04 19:45:51

标签: java javafx-8 fxml fxmlloader

我只是在学习,所以如果答案已经可以找到,请原谅我。我每天四处张望,但找不到我的问题的答案。

我的主要方法是加载登录页面,一切正常,我可以登录到我的应用程序。

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("/view/login.fxml"));
        primaryStage.setTitle("Contact Manager");
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }


    public static void main(String[] args) {

        launch(args);

    }
}

登录名将通过以下方法进行操作,并可以很好地加载下一个视图。

public void login(){

        String username = System.getProperty("user.name");
        String password = login_password.getText();

        if(username != "" && password != ""){

            User user = new User();

            user = user.getByUsername();

            if(user.getPassword().equals(password)){

                try {

                    AnchorPane pane = FXMLLoader.load(getClass().getResource("/view/contact_manager.fxml"));
                    rootPane.getChildren().setAll(pane);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {

                msgLabel.setText("Incorrect username or password!");

            }
        } else {

            msgLabel.setText("Username and password are required!");

        }
    }

FXML加载良好。

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane fx:id="rootPane" fx:controller="controller.ContactsController"  maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.202" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane prefHeight="70.0" prefWidth="600.0">
         <children>
            <Text layoutX="189.0" layoutY="42.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Contacts Manager">
               <font>
                  <Font name="Calibri" size="30.0" />
               </font>
            </Text>
         </children>
      </Pane>
      <AnchorPane fx:id="applicationPane" layoutY="70.0" prefHeight="530.0" prefWidth="600.0">
         <children>
            <TabPane prefHeight="530.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
              <tabs>
                <Tab fx:id="tab_contacts" text="Contacts">
                    <AnchorPane prefHeight="484.0" prefWidth="600.0" >
                        <fx:include source="contactsTab.fxml"/>
                     </AnchorPane>
                </Tab>
                <Tab fx:id="tab_profile" text="Profile">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                        <fx:include source="profileTab.fxml" />
                    </AnchorPane>
                  </content>
                </Tab>
              </tabs>
            </TabPane>
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

但是在下一个控制器中,当我使用以下方法打印applicationPane时

package controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;

import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;

import java.io.IOException;

public class ContactsController {

    @FXML AnchorPane applicationPane;
    @FXML MenuButton dropdown_contact_type;
    @FXML MenuItem select_person;
    @FXML MenuItem select_company;

    public void new_contact(){


        try {
            AnchorPane pane = FXMLLoader.load(getClass().getResource("/view/contact_form.fxml"));
            applicationPane.getChildren().setAll(pane);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

当我在contactsTab include中单击按钮时,得到以下错误。

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<VBox fx:id="contactsTab" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="506.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ContactsController">
   <children>
      <Pane prefHeight="53.0" prefWidth="600.0">
         <children>
            <TextField layoutX="14.0" layoutY="14.0" />
            <Button layoutX="173.0" layoutY="14.0" mnemonicParsing="false" text="Find" />
            <Button fx:id="new_contact" layoutX="561.0" layoutY="14.0" mnemonicParsing="false" onAction="#new_contact" text="+" />
            <MenuButton fx:id="dropdown_contact_type" layoutX="443.0" layoutY="14.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="107.0" text="Select Type:" textFill="#948f8f">
               <items>
                  <MenuItem fx:id="select_person" mnemonicParsing="false" onAction="#select_person" onMenuValidation="#select_person" text="Peron" />
                  <MenuItem fx:id="select_company" mnemonicParsing="false" onAction="#select_company" onMenuValidation="#select_company" text="Company" />
               </items>
            </MenuButton>
            <Region layoutX="212.0" layoutY="2.0" prefHeight="51.0" prefWidth="231.0" />
         </children>
      </Pane>
      <TableView prefHeight="458.0" prefWidth="600.0">
         <columns>
            <TableColumn prefWidth="153.0" text="Name" />
            <TableColumn prefWidth="147.0" text="E-mail" />
            <TableColumn prefWidth="159.0" text="Phone" />
            <TableColumn prefWidth="140.0" text="Mobile" />
         </columns>
      </TableView>
   </children>
</VBox>

出现以下错误:

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.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.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)
    ... 52 more
Caused by: java.lang.NullPointerException
    at controller.ContactsController.new_contact(ContactsController.java:25)
    ... 62 more

如果有人能指出我正确的方向,我将是一个非常快乐的人!

预先感谢

0 个答案:

没有答案