0

I'm trying to execute this code. It's a program that just lets the user enter the sum of two numbers. The two numbers are randomly chosen and theres a get method in another class that retrieves them. I tried to assign that method to the questionField label, but i keep getting a error when i try to run and compile. Can someone help me out and tell me what im doing wrong?

public class Assignment5 extends Application {

    // TODO: Instance Variables for View Components and Model
    private MathGame math1;
    private TextField answerField;
    private Label questionField;
    private Label answerRight;
    private Label answerWrong;

    // TODO: Private Event Handlers and Helper Methods
    private void mathHandler(ActionEvent e) {
        int n = Integer.parseInt(answerField.getText());
        math1.verify();
    }


    /**
     * This is where you create your components and the model and add event
     * handlers.
     *
     * @param stage The main stage
     * @throws Exception
     */
    @Override
    public void start(Stage stage) throws Exception {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 225); // set the size here
        stage.setTitle("Assignment5"); // set the window title here
        stage.setScene(scene);
        // TODO: Add your GUI-building code here

        // 1. Create the model
        math1 = new MathGame();
        // 2. Create the GUI components
        answerField = new TextField("");
        Button input = new Button("OK");
        questionField = new Label("");
        answerRight = new Label("Test");
        answerWrong = new Label("Test");

        // 3. Add components to the root
        root.getChildren().add(answerWrong);
        root.getChildren().add(answerRight);
        root.getChildren().add(questionField);
        root.getChildren().add(answerField);
        root.getChildren().add(input);
        //root.getChildren().add(input);

        // 4. Configure the components (colors, fonts, size, location)
        questionField.setLayoutY(100);
        questionField.setLayoutX(30);
        answerField.setLayoutX(100);
        answerField.setLayoutY(100);
        answerField.setPrefWidth(40);
        answerField.setPrefHeight(20);
        input.setLayoutX(160);
        input.setLayoutY(100);
        answerRight.setLayoutY(140);
        answerWrong.setLayoutY(165);

        // 5. Add Event Handlers and do final setup
        // 6. Show the stage
        stage.show();

    }

    /**
     * Make no changes here.
     *
     * @param args unused
     */
    public static void main(String[] args) {
        launch(args);
    }
}

and this is the error message i get when i run it

Exception in Application constructor
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:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Assignment5.Assignment5
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: Assignment5.MathGame
    at Assignment5.Assignment5.<init>(Assignment5.java:18)
    ... 13 more
Exception running application Assignment5.Assignment5
C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
Matt
  • 3,052
  • 1
  • 17
  • 30
rcv40
  • 37
  • 2
  • 8

1 Answers1

0

I see that you are using Netbeans IDE:

C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53

And if you read your exception carefully you will find a Runtime Exceptionthat said : Unable to construct Application instance: class Assignment5.Assignment5,and netbeans allows you to run the code even if certain classes are not compilable.According to Nick Fortescue and Viacheslav Vedenin answsers.

Because you did not post all code ,So i guess , It was the packaging issue.

So Try do this :

1-Clean and build your project again.

2- try to uncheck "Compile on save" setting in the project properties (Build -> Compiling).

I hope that is helpful for you.