-2

I'm just started to learn JavaFX. I want to create a Login Scene and my target is to write a inventory application. When I run the following code in eclipse with java 8 and I get an error. Please Help me. The fxml is generated using the scene builder.

Main.java:

package application;

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

public class Main extends Application {


@Override
       public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("LoginStage.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("HelloSwingNode Sample");
        stage.show();
    }

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

LoginStage.fxml:

<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.util.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"    minWidth="-Infinity" prefHeight="467.0" prefWidth="716.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.Controller">
   <children>
      <Pane layoutX="31.0" layoutY="22.0" prefHeight="413.0" prefWidth="644.0" style="-fx-background-color: #ffffff;">
         <children>
            <JFXTextField fx:id="userName" cacheShape="false" focusColor="#8fc9f5" layoutX="123.0" layoutY="147.0" maxWidth="386.0" minWidth="210.0" prefHeight="41.0" prefWidth="386.0" promptText="Username" unFocusColor="BLACK">
               <font>
                  <Font size="14.0" />
               </font>
           </JFXTextField>
            <JFXPasswordField fx:id="password" focusColor="#8fc9f5" layoutX="123.0" layoutY="221.0" maxWidth="386.0" minWidth="205.0" prefHeight="41.0" prefWidth="386.0" promptText="Password">
           <font>
                  <Font size="14.0" />
               </font>
            </JFXPasswordField>
            <JFXButton fx:id="login" alignment="CENTER" buttonType="RAISED" layoutX="123.0" layoutY="313.0" onAction="#makeLogin" prefHeight="41.0" prefWidth="386.0" ripplerFill="WHITE" style="-fx-background-color: #9999ff;" text="Login" textAlignment="CENTER" textFill="#e5e5ff">
               <font>
                 <Font name="System Bold" size="20.0" />
               </font>
            </JFXButton>a
            <Label layoutX="123.0" layoutY="21.0" prefHeight="74.0" prefWidth="272.0" style="-fx-background-color: #ffffff;" text="Welcome" textFill="#8cc9ff">
               <font>
                  <Font size="50.0" />
               </font>
            </Label>
                <Label layoutX="123.0" layoutY="95.0" prefHeight="31.0"     prefWidth="194.0" style="-fx-background-color: #ffffff;" text="To our inventory   system" textFill="#949292">
                   <font>
                       <Font size="17.0" />
                   </font>
                </Label>
             </children>
             <effect>
                <DropShadow />
             </effect>
           </Pane>
       </children>
    </AnchorPane>

my 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(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(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(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Kelvinyu/workspace/InvApp/bin/application/LoginStage.fxml:13

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at   javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.ClassNotFoundException: Main.Controller
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)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Exception running application application.Main
Kelvinyu1117
  • 509
  • 1
  • 5
  • 14
  • "I get an error". What error? What does it [tell you is going wrong](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors)? Post the stack trace in your question. – James_D Oct 05 '16 at 18:05
  • OK, I have posted the stack trace – Kelvinyu1117 Oct 06 '16 at 00:21
  • It can't find the controller class. Is the package name correct? – James_D Oct 06 '16 at 00:23

1 Answers1

0

The key lines here are:

FXML: fx:controller="Main.Controller"
Stacktrace: Caused by: java.lang.ClassNotFoundException: Main.Controller

The FXMLLoader is trying to find your Controller class which you declared in FXML via reflection. I suspect you don't have a class Main.Controller.

Currently the FXMLLoader assumes your project looks like this:

src
|-> Main (package)
|   |-> Controller.java (class)
|-> Main.java (class)
Jhonny007
  • 1,698
  • 1
  • 13
  • 33