线程中的异常" JavaFX应用程序线程"

时间:2017-04-27 17:18:47

标签: javafx

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:8413)
    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:381)
    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$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 48 more
Caused by: java.lang.NullPointerException
    at fxml.MainController.makeBtn(MainController.java:14)
    ... 58 more

我不明白这个错误。请帮忙。 THKS。

Main.java

package Code;

import java.io.IOException;

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

public class Main extends Application {
    private static Stage pStage;

    @Override
    public void start(Stage primaryStage) throws IOException {
        Main.pStage = primaryStage;

        showPartEditor();
        //showMakeEditor();
    }

    private void showPartEditor() throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/PartEditor.fxml"));
        Scene scene = new Scene(root);
        pStage.setTitle("Part Editor");
        pStage.setScene(scene);
        pStage.show();
    }

    public void showMakeEditor() throws IOException{

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("fxml/MakeEditor.fxml"));
        Parent root = loader.load();

        Stage addDialogStage = new Stage();
        addDialogStage.setTitle("Make Editor");
        addDialogStage.initModality(Modality.WINDOW_MODAL); // or Modality.NONE
        addDialogStage.initOwner(pStage);
        Scene scene = new Scene(root);
        addDialogStage.setScene(scene);
        addDialogStage.showAndWait();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

MainController.java

package fxml;

import java.io.IOException;

import Code.Main;
import javafx.fxml.FXML;

public class MainController{

    private Main main;

    @FXML
    private void makeBtn() throws IOException{
        main.showMakeEditor();
    }
}

PartEditor.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="260.0" prefWidth="480.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.MainController">
   <children>
      <Label layoutX="40.0" layoutY="30.0" text="Year:" />
      <Label layoutX="40.0" layoutY="60.0" text="Make:" />
      <Label layoutX="40.0" layoutY="90.0" text="Model:" />
      <Label layoutX="40.0" layoutY="120.0" text="Category:" />
      <Label layoutX="40.0" layoutY="150.0" text="Unit Price:" />
      <Label layoutX="40.0" layoutY="180.0" text="Part Name:" />
      <ComboBox layoutX="120.0" layoutY="26.0" prefWidth="70.0" />
      <ComboBox layoutX="120.0" layoutY="56.0" prefWidth="150.0" />
      <ComboBox layoutX="120.0" layoutY="86.0" prefWidth="150.0" />
      <ComboBox layoutX="120.0" layoutY="116.0" prefWidth="150.0" />
      <Button layoutX="280.0" layoutY="56.0" mnemonicParsing="false" onAction="#makeBtn" prefWidth="100.0" text="New Make..." />
      <Button layoutX="280.0" layoutY="86.0" mnemonicParsing="false" prefWidth="100.0" text="New Model..." />
      <Button layoutX="280.0" layoutY="116.0" mnemonicParsing="false" prefWidth="100.0" text="New Category..." />
      <TextField layoutX="120.0" layoutY="146.0" prefWidth="70.0" />
      <Label layoutX="233.0" layoutY="150.0" text="Part #:" />
      <TextField layoutX="280.0" layoutY="146.0" prefWidth="100.0" />
      <TextField layoutX="120.0" layoutY="176.0" prefHeight="25.0" prefWidth="343.0" />
      <Button layoutX="393.0" layoutY="220.0" mnemonicParsing="false" prefWidth="70.0" text="Close" />
      <Button layoutX="310.0" layoutY="220.0" mnemonicParsing="false" prefWidth="70.0" text="Summit" />
   </children>
</AnchorPane>

MakeEditor.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="100.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="24.0" layoutY="23.0" text="Make:" />
      <TextField layoutX="68.0" layoutY="19.0" prefHeight="25.0" prefWidth="159.0" />
      <Button layoutX="97.0" layoutY="61.0" mnemonicParsing="false" prefWidth="60.0" text="OK" />
      <Button layoutX="167.0" layoutY="61.0" mnemonicParsing="false" prefWidth="60.0" text="Cancel" />
   </children>
</AnchorPane>

0 个答案:

没有答案