StackOverflowError (java fx)

StackOverflowError (java fx)

我正在编写一个代码,其中 class 用于绘制一条线,另一个 class 用于显示该线。 class 的目的是让我轻松编写多行代码并使我的代码更有条理。但出于某种原因,每当我 运行 它时,我都会收到 Whosebug 错误。我不确定为什么会这样。它还给了我一个 RunTimeException 和许多不同的错误消息,例如“应用程序启动方法中的异常”。我不确定这些是什么意思。

这是它给我的错误信息:

    Exception in Application start method
    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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
    Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.WhosebugError
    at Line.<init>(Main.java:133)
    at Line.<init>(Main.java:133)
    at Line.<init>(Main.java:133) //this just repeats a bunch of times
Exception running application Main

这是代码:

import javafx.scene.shape.Ellipse;
import javafx.scene.Node;
import javafx.scene.paint.Paint;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;

class Line{

    public Line line;

    public Line(int x1,  int y1, int x2, int y2){

        Line line = new Line(x1, y1, x2, y2);
    }

    public void setFill(Paint p) {
        line.setFill(p);
    }

}

public class Main extends Application {
    private Pane root;
    @Override
    public void start(Stage primaryStage) {
        root = new Pane();

        Line line1 = new Line(250,250,450,450);

        root.getChildren().addAll((Collection<? extends Node>) line1);
        line1.setFill(Color.BLACK);

        Scene scene = new Scene(root, 500, 500);
        primaryStage.setTitle("Empty");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {

        Application.launch(args);
    }


}

那是因为这永远不会结束。

public Line(int x1,  int y1, int x2, int y2){

        Line line = new Line(x1, y1, x2, y2);
    }

在创建新对象时,您正在调用构造函数。在其中,它正在创建一个对象,这会导致再次调用构造函数等等。
所以改为这样做:

class Line{

    int x1,x2,y1,y2;

    public Line(int x1,  int y1, int x2, int y2){

        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

}

您不需要创建 setFill() 函数,因为它是导入的。只需使用该功能即可。