-1
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.*;

public class Drawing extends Application{
private BorderPane mainPane;
private HBox statusBox;
private Text statusText;

public void start(Stage primaryStage) {

    statusText = new Text("OFF");
    statusBox = new HBox(statusText);
    statusBox.setAlignment(Pos.CENTER);
    mainPane.setTop(statusBox);          //nullPointerException here

    Scene scene = new Scene(mainPane, 1000, 1000);
    primaryStage.setTitle("Draw Something");
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.show();
}

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

I want to create a BorderPane and set the top portion for some text. I created an HBox to put inside. I don't know why this error occurs.

carmen
  • 11
  • 3

1 Answers1

4

Your mainPane.setTop(statusBox) has not been instantiated yet. Instantiate mainPane first by mainPane = new BorderPane() before using it.

Awan Biru
  • 373
  • 2
  • 10