I am writing a code with a class for drawing a line and another class to display the line. The point of the class is for me to easily write multiple lines and make my code a bit more organized. But for some reason, I am getting a StackOverflow error whenever I run it. I am unsure on why this is happening. it also gives me a RunTimeException and a lot of different error messages like "Exception in application start method". I am unsure on what those are supposed to mean.
This is the error messages it gives me:
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$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.StackOverflowError
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
This is the code:
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);
}
}
That is because this never ends.
public Line(int x1, int y1, int x2, int y2){
Line line = new Line(x1, y1, x2, y2);
}
While creating a new object, you are calling the constructor. And within it, it's creating an object, which leads to calling constructor again and so on.
So do this instead:
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;
}
}
You don't need to create setFill()
function as it's imported. Simply use the function.