user3557576
user3557576

Reputation: 27

JavaFx erron in running

i have got a problem with javafx. i created my fxml file in SceneBuilder and put it in the same directory with package folder. here are codes:

public class Main  extends Application    {



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

@Override
public void start(Stage window) throws Exception {
    Pane mainPane = (Pane)FXMLLoader.load(Main.class.getResource("../sas.fxml"));


    Scene scene = new Scene(mainPane);
    window.setScene(scene);
    window.show();
}
}

when i run this it gives me this error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1323468230.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/H:/Java%20projects/JavaFx/bin/sas.fxml:7

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1393559157.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/200091476.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Caused by: java.lang.ClassNotFoundException: MyController
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more
Exception running application javafx.Main

i searched solution online, most of them says that problem can be with url. but as you see in first code i wrote it like (../sas.fxml) because it is not in the same folder with java files, it is in the same folder with package folder. so any idea how to solve it? appreciate any solution

FXML File

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="185.0" prefWidth="349.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
   <children>
      <TextField fx:id="username" layoutX="44.0" layoutY="35.0" promptText="username" />
      <Button fx:id="login" layoutX="224.0" layoutY="35.0" mnemonicParsing="false" onAction="#loginFucntion" text="Login" />
   </children>
</Pane>

MyController.java

package javafx;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class MyController {

    @FXML
    private Button login;

    @FXML
    private TextField username;

    @FXML
    void loginFucntion(ActionEvent event) {

    }

}

Upvotes: 0

Views: 64

Answers (2)

James_D
James_D

Reputation: 209340

The fx:controller attribute requires the fully qualified name of the class. Since you placed MyController in a package called javafx (which, by the way, you shouldn't: that is a protected package name, so you should choose something else that is specific to your company/organization etc), you would need

fx:controller="javafx.MyController"

Since you specified just fx:controller="MyController", the FXMLLoader is looking in the default package (i.e. in the root of the classpath) for a class called MyController. Since it can't find it there (it is in a different package), it is giving you a ClassNotFoundException.

Upvotes: 2

Omar El Halabi
Omar El Halabi

Reputation: 2128

Consider this line /H:/Java%20projects/JavaFx/bin/sas.fxml:7

I think the folder name is "Java projects". Try renaming it to "JavaProjects" or "Java_projects". The space character is converted to %20 which causes the java.lang.ClassNotFoundException to be thrown

Upvotes: 0

Related Questions