JavaFX Load Exception

ulqy picture ulqy · Oct 1, 2014 · Viewed 8.3k times · Source

I'm trying to make a basic calculator application with JavaFX but I'm having trouble launching the application.

This is the error I get

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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/815033865.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 

/path/.../view/CalculatorScreen.fxml:10

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at calculator.Calculator.start(Calculator.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1284973402.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1127003017.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.Button field controller.CalculatorScreenController.zeroButton to javafx.scene.control.Button
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:758)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1155)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:853)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:747)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
... 17 more
Exception running application calculator.Calculator

This is how my packages are set up.

calculator
    Calculator.java
controller
    CalculatorScreenController.java
view
    CalculatorScreen.fxml

My Calculator.java

package calculator;

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

public class Calculator extends Application {

@Override
public void start(Stage stage) throws Exception
{
    FXMLLoader fxmlLoader =  new FXMLLoader( getClass().getResource( "/view/CalculatorScreen.fxml" ));
    Parent root = fxmlLoader.load();
    CalculatorScreenController controller = (CalculatorScreenController) fxmlLoader.getController();

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

My CalculatorScreenController.java

package controller;

import calculator.Calculator;
import java.awt.Button;
import java.awt.Label;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class CalculatorScreenController implements Initializable{

@FXML
private Button oneButton;
@FXML
private Button twoButton;
@FXML
private Button threeButton;
@FXML
private Button fourButton;
@FXML
private Button fiveButton;
@FXML
private Button sixButton;
@FXML
private Button sevenButton;
@FXML
private Button eightButton;
@FXML
private Button nineButton;
@FXML
private Button zeroButton;

@FXML
private Button dotButton;
@FXML
private Button plusButton;
@FXML
private Button minusButton;
@FXML
private Button multiplyButton;
@FXML
private Button divideButton;
@FXML
private Button equalsButton;

@FXML
private Button clearButton;

@FXML
private Label displayLabel;

@Override
public void initialize(URL url, ResourceBundle rb) {
    displayLabel.setText("500");
}
}

I know I don't have much code written yet, as of now I'm just trying to figure out how to run the application. I'm new at all of this so any help would be appreciated, thanks!

Answer

James_D picture James_D · Oct 1, 2014

You have the wrong imports in your controller. java.awt.Button and java.awt.Label should be javafx.scene.control.Button and javafx.scene.control.Label