JavaFX:“线程 "JavaFX Application Thread" java.lang.RuntimeException 中的异常:java.lang.reflect.InvocationTargetException”JavaFX 上的错误

JavaFX: " Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException" Error on JavaFX

在我的 DatePicker 上选择日期后,我看到了这些错误:

E:\все jdk\jdk1.8.0_152\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.8\bin" -Dfile.encoding=UTF-8 -classpath "E:\все jdk\jdk1.8.0_152\jre\lib\charsets.jar;E:\все jdk\jdk1.8.0_152\jre\lib\deploy.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\dnsns.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\jaccess.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\localedata.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\nashorn.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunec.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\zipfs.jar;E:\все jdk\jdk1.8.0_152\jre\lib\javaws.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jce.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jfr.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jfxswt.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jsse.jar;E:\все jdk\jdk1.8.0_152\jre\lib\management-agent.jar;E:\все jdk\jdk1.8.0_152\jre\lib\plugin.jar;E:\все jdk\jdk1.8.0_152\jre\lib\resources.jar;E:\все jdk\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\ogobe\IdeaProjects\YSK_1\out\production\YSK_1;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.8\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain sample.Main
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 com.sun.javafx.scene.control.skin.DatePickerSkin.handleControlPropertyChanged(DatePickerSkin.java:147)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener(BehaviorSkinBase.java:197)
    at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler.changed(MultiplePropertyChangeListenerHandler.java:55)
    at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
    at com.sun.javafx.scene.control.skin.DatePickerContent.selectDayCell(DatePickerContent.java:689)
    at com.sun.javafx.scene.control.skin.DatePickerContent.lambda$createDayCells4(DatePickerContent.java:731)
    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$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access00(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$handleMouseEvent3(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$null7(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:1769)
    ... 60 more
Caused by: java.lang.NullPointerException
    at sample.Controller.getDate(Controller.java:72)
    ... 70 more
number of years: 1
number of months: 0
number of days: 0

Process finished with exit code 0

Controller.java:

package sample;

import java.net.URL;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.time.Period;
import javax.swing.*;


public class Controller {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private DatePicker date_1;

    @FXML
    private DatePicker date_2;

    @FXML
    private Button buttonAdd;

    @FXML
    private ChoiceBox<?> choiceBox;

    @FXML
    private TextField lavozm;

    @FXML
    private Label year;

    @FXML
    private Label month;

    @FXML
    private Label yearOfIIN;

    @FXML
    private Label monthOfXis;

    @FXML
    private Label dayOfXis;


    public void getDate(ActionEvent event) {

        LocalDate dt1 = date_1.getValue();
        Calendar c =  Calendar.getInstance();
        c.set(dt1.getYear(), dt1.getMonthValue(), dt1.getDayOfMonth());
        Date date1 = c.getTime();

        LocalDate dt2 = date_2.getValue();
        Calendar c2 =  Calendar.getInstance();
        c2.set(dt2.getYear(), dt2.getMonthValue(), dt2.getDayOfMonth());
        Date date2 = c.getTime();


        Period age = Period.between(dt1, dt2);


        int years = age.getYears();
        int months = age.getMonths();
        int days = age.getDays();



        System.out.println("number of years: " + years);
        System.out.println("number of months: " + months);
        System.out.println("number of days: " +  days);

    }

    @FXML
    void initialize() {

    }
}

Main.java:

package sample;

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("sample.fxml"));
        primaryStage.setTitle("YSK");
        primaryStage.setScene(new Scene(root, 800, 550));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


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

sample.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="829.0" xmlns="javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="68.0" prefWidth="829.0" style="-fx-background-color: #1d90ed;">
         <children>
            <TextField layoutX="521.0" layoutY="21.0" prefHeight="16.0" prefWidth="269.0" promptText="Ходим исм шарфи" style="-fx-background-color: #EAFAFA;" />
         </children>
      </AnchorPane>
      <DatePicker fx:id="date_1" layoutX="14.0" layoutY="123.0" onAction="#getDate" prefHeight="27.0" prefWidth="149.0" promptText="Боши" />
      <DatePicker fx:id="date_2" layoutX="176.0" layoutY="124.0" onAction="#getDate" prefHeight="27.0" prefWidth="149.0" promptText="Якуни" />
      <Button fx:id="buttonAdd" layoutX="387.0" layoutY="172.0" minWidth="26.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="56.0" style="-fx-background-color: #1d90ed;" text="+" />
      <ChoiceBox fx:id="choiceBox" layoutX="332.0" layoutY="124.0" prefHeight="26.0" prefWidth="48.0" />
      <Text layoutX="28.0" layoutY="438.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Календар хисобида:" />
      <Text layoutX="65.0" layoutY="467.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Имтиез блан:" />
      <TextField fx:id="lavozm" layoutX="395.0" layoutY="124.0" prefHeight="26.0" prefWidth="185.0" promptText="Лавозм" />
      <Text layoutX="624.0" layoutY="143.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
      <Text layoutX="750.0" layoutY="144.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
      <Text layoutX="689.0" layoutY="143.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
      <Label fx:id="year" layoutX="587.0" layoutY="126.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="month" layoutX="654.0" layoutY="127.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="month" layoutX="715.0" layoutY="127.0" prefHeight="25.0" prefWidth="28.0" />
      <Text layoutX="187.0" layoutY="437.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
      <Text layoutX="313.0" layoutY="438.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
      <Text layoutX="252.0" layoutY="437.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
      <Label fx:id="year" layoutX="150.0" layoutY="420.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="month" layoutX="217.0" layoutY="421.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="month" layoutX="278.0" layoutY="421.0" prefHeight="25.0" prefWidth="28.0" />
      <Text layoutX="191.0" layoutY="466.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
      <Text layoutX="317.0" layoutY="467.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
      <Text layoutX="256.0" layoutY="466.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
      <Label fx:id="yearOfIIN" layoutX="154.0" layoutY="449.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="monthOfXis" layoutX="221.0" layoutY="450.0" prefHeight="25.0" prefWidth="28.0" />
      <Label fx:id="dayOfXis" layoutX="282.0" layoutY="450.0" prefHeight="25.0" prefWidth="28.0" />
   </children>
</AnchorPane>

怎么回事

dt2 为 null,这是有道理的,因为对于 date_1 和 date_2 日期选择器,getDate 方法作为 fxml 中引用的 onAction 处理程序调用。

无论您先使用哪一个来选择日期,另一个选择的日期都将为空值,因为您一次只能选择一个日期。

如何修复

如果 date_1.getValue() 或 date_2.getValue() 为 null,则应将 getDate 方法更改为退出(通过 return)。

该方法似乎是在计算两个日期之间的时间段(这对于空日期没有意义)。

一旦用户选择了两个日期,通话就会接通,并且会正确计算时间段。

在getDate()的开头写:

if (date_1.getValue() == null || date_2.getValue() == null) {
    System.out.println(“Both dates not yet chosen, will not calculate period”);
    return;
}