0

I don't know how to debug this, and I've used two different books and online resources. My code won't even run and I don't get why it won't start as a windowed application. Source below the error.

The stack trace:

Exception in thread "main" 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:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    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:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    ... 5 more

My code:

import javafx.stage.Window;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public abstract class window extends Application {

    public void main( String args[]) {
        Stage primaryStage = null;
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(10));
        pane.setMaxHeight(1080);
        pane.setMaxWidth(1920);
        pane.setHgap(5);
        pane.setVgap(5);

        pane.getChildren().addAll(new Button("Initiate Virtual Reality"),
                new TextField(), new Label("Enter Response"), new Button("Send "
            + "Response") );
        Scene scene = new Scene(pane, 720, 1280);
        primaryStage.setTitle("Hello World! Goodbye!"); //Make evil statement
        primaryStage.setScene(scene); //Place scene in the stage
        primaryStage.show(); //Display the stage
        Application.launch(args);
    }

}
avojak
  • 2,342
  • 2
  • 26
  • 32
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Juan Carlos Mendoza Mar 01 '18 at 19:44
  • A `NPE` for sure, but the root cause is unclear in the stacktrace shown. Also see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Kayaman Mar 01 '18 at 19:47
  • 1
    You forget to assign `primaryStage` a value before calling methods on it. – nitind Mar 01 '18 at 20:12

1 Answers1

0

This is the offending line:

primaryStage.setTitle("Hello World! Goodbye!"); //Make evil statement

because you just have:

Stage primaryStage = null;

with no in between actual object assignment.

I know nothing about the javafx package and how Stages are created, but this seems like a nice example: https://examples.javacodegeeks.com/desktop-java/javafx/javafx-stage-example/

gvasquez
  • 1,919
  • 5
  • 27
  • 41