Home > database >  JavaFX java.lang.reflect.InvocationTargetException when launching textbook file
JavaFX java.lang.reflect.InvocationTargetException when launching textbook file

Time:07-21

Ive brought my textbook example into eclipse in a new JavaFX project. Code, error and other screenshots can be found at the bottom. What follows is a list of things ive tried:

I have --module-path "C:\Program Files\Java\javafx-sdk-18.0.1\lib" --add-modules javafx.controls,javafx.fxml in my run configurations.

I have added all of the .jars into a library titled "JAVAFX" that is in the build path

I have gone into that library and added the location of src.zip as well as downloaded the javadocs from the website and added those locations as well.

I do not know what else to do, please help.

Code:

DrawLines.java:

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

public class DrawLines extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      // loads DrawLines.fxml and configures the DrawLinesController
      Parent root = 
         FXMLLoader.load(getClass().getResource("Drawlines.fxml"));

      Scene scene = new Scene(root); // attach scene graph to scene
      stage.setTitle("Draw Lines"); // displayed in window's title bar
      stage.setScene(scene); // attach scene to stage
      stage.show(); // display the stage
   }

   public static void main(String[] args) {
      launch(args); // create a DrawLines object and call its start method
   }
}

DrawLinesController.java

 package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;

public class DrawLinesController {

    @FXML
    private Canvas canvas;

    @FXML
    void drawLinesButtonPressed(ActionEvent event) {
        
    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.strokeLine(0, 0, canvas.getWidth(), canvas.getHeight());


    gc.strokeLine( canvas.getWidth(), 0,0, canvas.getHeight());
    }
}

Drawlines.FXML

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

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="DrawLinesController">
   <top>
      <ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
          <Button mnemonicParsing="false" text="DrawLines" />
        </items>
      </ToolBar>
   </top>
   <center>
      <Canvas fx:id="canvas" height="300.0" width="300.0" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

Error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Xxmoz/OneDrive/Documents/git-hub-repos/Eclipse Projects/Drawlines/bin/application/Drawlines.fxml:9

    at [email protected]/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at [email protected]/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:933)
    at [email protected]/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
    at [email protected]/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
    at [email protected]/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
    at [email protected]/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at application.DrawLines.start(DrawLines.java:14)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: DrawLinesController
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at [email protected]/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:931)
    ... 22 more
Exception running application application.DrawLines

CodePudding user response:

The fx:controller reference needs to be a fully qualified name (including the package).

Change:

fx:controller="DrawLinesController"

to:

fx:controller="application.DrawLinesController"
  • Related