如何从URL读取数据并在折线图中使用?获取JAVAFX错误

如何从URL读取数据并在折线图中使用?获取JAVAFX错误,java,javafx,Java,Javafx,我创建了一个应用程序,它使用java和javafx从IntelliJ上的在线API读取比特币的价格。编译器不会识别任何错误,fxml文件中也没有错误,但当我运行应用程序时,会出现多个javafx错误。我怎样才能修好它?我尝试了不同的方法,但fxml文件中似乎有东西断开了连接。这个错误似乎也来自我的loadChart()方法,因为当我删除它时,一切都很顺利。如何在此处输入代码我将Y值链接到API中的价格而不出错?你能帮忙吗 多谢各位 Main class: package sample; im

我创建了一个应用程序,它使用java和javafx从IntelliJ上的在线API读取比特币的价格。编译器不会识别任何错误,fxml文件中也没有错误,但当我运行应用程序时,会出现多个javafx错误。我怎样才能修好它?我尝试了不同的方法,但fxml文件中似乎有东西断开了连接。这个错误似乎也来自我的loadChart()方法,因为当我删除它时,一切都很顺利。
如何在此处输入代码
我将Y值链接到API中的价格而不出错?你能帮忙吗

多谢各位

Main class:

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("Bitcoin Tracker");
    primaryStage.setScene(new Scene(root, 600, 600));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
 }
}
控制器类:

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.*;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.stage.*;
import javafx.application.Application;

public class Controller {

BTC my_price;
@FXML // fx:id="price_label";
        Label price_label;


High  highprice;
@FXML // fx:id="high_label";
     Label high_label;


Low lowprice;
@FXML // fx:id="low_price"
        Label low_label;

VolumeFrom vfrom;
@FXML // fx:id="vfrom_label"
        Label vfrom_label;

VolumeTo vto;
@FXML // fx:id="vto_label"
        Label vto_label;

@FXML // fx:id="chart";
        LineChart<String, Number> line_chart;

ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);

public Controller(){
    my_price = new BTC();

    highprice = new High();

    lowprice = new Low();

    vfrom = new VolumeFrom();

    vto = new VolumeTo();

    Controller ctrl = this;
    exec.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            ctrl.refreshCurrency();
        }
    }, 1, 1, TimeUnit.MINUTES);
}

public void refreshCurrency() {
    System.out.println("Refreshing...");

    CompletableFuture<Double> future = new CompletableFuture<Double>();

    future.supplyAsync(() -> {
        my_price.getCurrency();
        Platform.runLater(() -> {
            price_label.setText(new Double(my_price.price).toString());
        });

        return 1.0;
    });


    future.supplyAsync(() -> {
        highprice.getHigh();
        Platform.runLater(() -> {
            high_label.setText(new Double(highprice.highprice).toString());
        });

        return 1.0;
    });

    future.supplyAsync(() -> {
        lowprice.getLow();
        Platform.runLater(() -> {
            low_label.setText(new Double(lowprice.lowprice).toString());
        });

        return 1.0;
    });

    future.supplyAsync(() -> {
        vfrom.getVfrom();
        Platform.runLater(() -> {
            vfrom_label.setText(new Double(vfrom.vfrom).toString());
        });

        return 1.0;
    });

    future.supplyAsync(() -> {
        vto.getVto();
        Platform.runLater(() -> {
            vto_label.setText(new Double(vto.vto).toString());
        });

        return 1.0;
    });

    System.out.println("Refreshing high...");
}

public void loadChart(){

    line_chart.setTitle("BTC");

    XYChart.Series series = new XYChart.Series();

double BTCprice = Double.parseDouble(BTC.readFromAPI());

    Calendar c = Calendar.getInstance();
    Date start = new Date(1521171240000L);
    System.out.println(start);

    series.getData().add(new XYChart.Data<>(c.getTime().toString(),(BTCprice)*1000));
    c.add(Calendar.HOUR_OF_DAY, 1);


    line_chart.getData().add(series);
    line_chart.getXAxis().setLabel("Time");
    line_chart.getXAxis().setLabel("Price");

}

}
FXML文件:

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

<?import javafx.scene.chart.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <Pane onMouseClicked="#refreshCurrency" prefHeight="472.0" prefWidth="626.0">
         <children>
            <Label layoutX="213.0" layoutY="22.0" text="Bitcoin Tracker">
               <font>
                  <Font name="System Bold" size="24.0" />
               </font>
            </Label>
            <Label layoutX="34.0" layoutY="24.0" text="BTC">
               <font>
                  <Font size="22.0" />
               </font>
            </Label>
            <Label fx:id="price_label" layoutX="95.0" layoutY="25.0" onMouseClicked="#refreshCurrency" text="Label">
               <font>
                  <Font size="21.0" />
               </font>
            </Label>
            <Label layoutX="39.0" layoutY="84.0" text="Volume from:" />
            <Label layoutX="39.0" layoutY="114.0" text="Volume to:" />
            <Label fx:id="vfrom_label" layoutX="119.0" layoutY="84.0" onMouseClicked="#refreshCurrency" text="Label" />
            <Label fx:id="vto_label" layoutX="119.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
            <Label layoutX="479.0" layoutY="84.0" text="High" />
            <Label layoutX="479.0" layoutY="114.0" text="Low" />

            <Label fx:id="high_label" layoutX="526.0" layoutY="84.0" text="Label" />
            <Label fx:id="low_label" layoutX="526.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
            <LineChart fx:id="line_chart" layoutX="40.0" layoutY="171.0" onMouseClicked="#loadChart" prefHeight="273.0" prefWidth="582.0">
              <xAxis>
                <CategoryAxis side="BOTTOM" />
              </xAxis>
              <yAxis>
                <NumberAxis prefHeight="232.0" prefWidth="25.0" side="LEFT" />
              </yAxis>
            </LineChart>
         </children>
      </Pane>
   </children>
</GridPane>

要使用HTTPS调用api,请使用类似ApacheHTTP客户端的HTTP客户端

谢谢。我不熟悉HTTP,请问我怎么做?谢谢。我找到了它,但我已经可以通过我使用的方法读取URL,但不知何故,我的折线图方法无法将其链接到图表。因此,我认为问题在于折线图方法,而不是URL的读取?分离并征服:将其简化为一个最小的示例,一次只显示一个方面(fxml、读取数据、可视化数据…),这也是为了您自己;)请阅读以获取有关如何添加的帮助,这看起来不像完整的堆栈跟踪<代码>调用TargetExceptions应该有一个根本原因。
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <Pane onMouseClicked="#refreshCurrency" prefHeight="472.0" prefWidth="626.0">
         <children>
            <Label layoutX="213.0" layoutY="22.0" text="Bitcoin Tracker">
               <font>
                  <Font name="System Bold" size="24.0" />
               </font>
            </Label>
            <Label layoutX="34.0" layoutY="24.0" text="BTC">
               <font>
                  <Font size="22.0" />
               </font>
            </Label>
            <Label fx:id="price_label" layoutX="95.0" layoutY="25.0" onMouseClicked="#refreshCurrency" text="Label">
               <font>
                  <Font size="21.0" />
               </font>
            </Label>
            <Label layoutX="39.0" layoutY="84.0" text="Volume from:" />
            <Label layoutX="39.0" layoutY="114.0" text="Volume to:" />
            <Label fx:id="vfrom_label" layoutX="119.0" layoutY="84.0" onMouseClicked="#refreshCurrency" text="Label" />
            <Label fx:id="vto_label" layoutX="119.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
            <Label layoutX="479.0" layoutY="84.0" text="High" />
            <Label layoutX="479.0" layoutY="114.0" text="Low" />

            <Label fx:id="high_label" layoutX="526.0" layoutY="84.0" text="Label" />
            <Label fx:id="low_label" layoutX="526.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
            <LineChart fx:id="line_chart" layoutX="40.0" layoutY="171.0" onMouseClicked="#loadChart" prefHeight="273.0" prefWidth="582.0">
              <xAxis>
                <CategoryAxis side="BOTTOM" />
              </xAxis>
              <yAxis>
                <NumberAxis prefHeight="232.0" prefWidth="25.0" side="LEFT" />
              </yAxis>
            </LineChart>
         </children>
      </Pane>
   </children>
</GridPane>
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    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:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3589)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3517)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3885)
    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.reflect.InvocationTargetException
    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)
    ... 31 more