Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:07:37+00:00 2026-06-09T19:07:37+00:00

I want to make an application in javaFX 2 which opens as a smaller

  • 0

I want to make an application in javaFX 2 which opens as a smaller login window, then, when you put in correct data, it takes you to bigger main window. Both are designed in fxml and events are handled within java code.

Yes, I know, it is almost the same as the application in samples and I’ve tried to do what I want and it worked there.

Now, when I did the same in my project, I hit a problem when I want to change the value of stage.

As you can see in the code below, I have global variable and I set the value of primaryStage in start method to it. Just as a test, I print it out at end of start method and the value is set.

Then, when I try to use it when button is clicked(method buttonClick), the value of stage variable is null, therefore I cannot use it to resize window or anything else.

My question is why is stage variable value reseted despite that I don’t use change anything between the two prints?

This code is sample of what I’ve tried, I’ve just cut out all code which is not crucial to understand how my application works.

public class App extends Application {

    private Stage stage;
    @FXML
    private AnchorPane pane;

    @Override
    public void start(Stage primaryStage) {
        try {
            stage = primaryStage; // Set the value of primaryStage to stage
            primaryStage.setScene(new Scene(openScene("Login"))); // Load Login window
            primaryStage.show(); // Show the scene
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(stage);// <-- Here it has the value of primaryStage obviously
    }

    @FXML
    void buttonClick(ActionEvent event) throws IOException {
    // Note that even if I try to print here, the value of stage is still
    // null, so the code doesn't affect it
    // Also, this loads what I want, I just can't change the size.
        try{
           pane.getChildren().clear(); // Clear currently displayed content
           pane.getChildren().add(openScene("MainScene")); // Display new content
           System.out.println(stage); // <-- Here, output is null, but I don't know why
           stage.setWidth(500); // This line throws error because stage = null
        } catch (IOException ex) {
           Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }

    }  

    public Parent openScene(String name) throws IOException {
        //Code from FXML login example
        Parent parent = (Parent) FXMLLoader.load(PrijavnoOkno.class.getResource(name
                + ".fxml"), null, new JavaFXBuilderFactory());
        return parent;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:07:38+00:00Added an answer on June 9, 2026 at 7:07 pm

    Although it is not clear by whom and where the buttonClick action method is called, I pressume it is a login button’s action in Login.fxml. Also I assume you have defined the App (a.k.a PrijavnoOkno) as a controller of this Login.fxml.
    According to these assumptions, there are 2 instances of App.class:
    one created when the app starts up and where the stage variable is assigned with primary stage in start() method,
    and another instance created by FXMLLoader (while loading Login.fxml) and where the stage variable is not assigned and thus NPE.
    One of the right ways can be, create a new Controller class for Login.fxml, call your login action in it. Access the global stage (by making it static in App) from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make an application in C or C++ which have to monitor
I want to make an console application of c which can run other applications
I want to make an application with plugins that also can have plugins (which
I want to make an application which prevents other applications to launch without user
I want to make an application which has Tab bar . In each TabBar
I want to make bigger application that stores multiple avatars for each registered user
I want to make application in symbian OS mobile which can work in all
I want to make application such that,when user opens application,it will show a google-map
I want to make an application in which I need a functionality of automatic
I want to make application in which i want to use camera as after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.