0

enter image description hereI am opening a Login JavaFX page from UserController.java and it has its control in LoginController.java.After processing it in LoginController.java and I am executing a close method to close the stage declared in UserController(which contains the stage for Login) but it throws NullPointerException

public class UserMenuController {
    @FXML
    private MenuItem Login;
    Stage loginStage;

    public void LoginPage(ActionEvent actionEvent) throws IOException {
        loginStage=new Stage();
        Parent loginFXML=FXMLLoader.load(getClass().getResource("/login.fxml"));
        loginStage.setTitle("Login");
        Scene loginScene=new Scene(loginFXML);
        loginStage.setScene(loginScene);
        loginStage.show();

    }
    public void shutLoginStage(){
        loginStage.close();
    }
}

public class LoginController implements Initializable {
    @FXML
    private WebView view;
    UserMenuController u=new UserMenuController();

    public void initialize(URL location, ResourceBundle resources) {
        view.getEngine().load("https://kite.trade/connect/login?v=3&api_key=kpnnt4xthv187j8p");
        view.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {[enter image description here][1]
            //Gets the URL  Request Token
            if (newValue.equals(Worker.State.FAILED)) {
                //System.out.println(view.getEngine().getLocation());
                System.out.println(view.getEngine().getLocation());
                u.shutLoginStage();

            }

        });
  • Since you have the `Controller` open, why not use the `loginStage` to close itself? – SedJ601 Aug 24 '18 at 13:36
  • The way your program is set up allows you to open more than one login stage on every button click. If you make your stage effectively final, that solves your problem. – SedJ601 Aug 24 '18 at 13:55

1 Answers1

0

Make Stage loginStage = new Stage(); Global.

MCVE

Controller

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private Label label;

    Stage loginStage = new Stage();

    ;

    @FXML
    private void handleButtonAction(ActionEvent event)
    {
        try {
            Parent loginFXML = FXMLLoader.load(getClass().getResource("Login.fxml"));
            loginStage.setTitle("Login");
            Scene loginScene = new Scene(loginFXML);
            loginStage.show();
        }
        catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    private void shutLoginStage(ActionEvent event)
    {
        if (loginStage.isShowing()) {//If the loginStage is showing, close it.
            loginStage.close();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }

}

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="javafxapplication246.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <Button layoutX="133.0" layoutY="137.0" mnemonicParsing="false" onAction="#shutLoginStage" text="Close" />
    </children>
</AnchorPane>

**Login Controller**

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author blj0011
 */
public class LoginController implements Initializable
{

    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }

}

Login FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication246.LoginController">

</AnchorPane>

Main

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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication246 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

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

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59