0
    package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Main extends Application {

    Stage window1;
    Stage window2;

    Scene mainW;
    Scene fileW;

    Pane main;
    GridPane file;

    @FXML
    private GridPane mainWindow;

    @FXML
    private GridPane fileWindow;

    @Override
    public void start(Stage primaryStage) throws Exception{
        /*window1 = primaryStage;

        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
        window1.setTitle("Sports League Essential");
        window1.setScene(new Scene(root, 750, 500));
        window1.show();
        */


        FXMLLoader loader1 = new FXMLLoader();
        loader1.setLocation(Main.class.getResource("Schedule.fxml"));
        file = loader1.load();

        fileW = new Scene(file);

        window1 = primaryStage;
        window1.setTitle("Sports League Essential");
        showMainView();
    }


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

    @FXML
    public void openNewFile() throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("Schedule.fxml"));
        window1.setTitle("Sports League Essential");
        window1.setScene(new Scene(root, 750, 500));
        window1.show();


    }

    public void showMainView() throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("MainWindow.fxml"));
        main = loader.load();

        Scene scene = new Scene(main);
        window1.setScene(scene);
        window1.show();
    }

    public void showFileView() throws Exception {

        window1.setScene(fileW);
        window1.show();
    }

}

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>


<Pane fx:id="" prefHeight="924.0" prefWidth="1283.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
   <children>
      <MenuBar prefHeight="0.0" prefWidth="1283.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
                  <MenuItem id="newFile" mnemonicParsing="false" onAction="#showFileView" text="New..." />
                  <MenuItem mnemonicParsing="false" text="Open..." />
                  <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</Pane>


    enter code here

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

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane fx:id="fileWindow" alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
    <columnConstraints>
        <ColumnConstraints />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
    </rowConstraints>
    <children>
        <Pane prefHeight="924.0" prefWidth="1283.0">
            <children>
                <MenuBar prefHeight="0.0" prefWidth="1283.0">
                    <menus>
                        <Menu mnemonicParsing="false" text="File">
                            <items>
                                <MenuItem mnemonicParsing="false" onAction="#openNewFile" text="New..." />
                                <MenuItem mnemonicParsing="false" text="Open..." />
                                <MenuItem mnemonicParsing="false" text="Close" />
                            </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Edit">
                            <items>
                                <MenuItem mnemonicParsing="false" text="Delete" />
                            </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Help">
                            <items>
                                <MenuItem mnemonicParsing="false" text="About" />
                            </items>
                        </Menu>
                    </menus>
                </MenuBar>
            <TableView id="teamInfoTable" layoutX="5.0" layoutY="58.0" prefHeight="679.0" prefWidth="1275.0">
              <columns>
                <TableColumn prefWidth="516.0" text="Team" />
                <TableColumn minWidth="0.0" prefWidth="230.0" text="Wins" />
                  <TableColumn prefWidth="250.0" text="Losses" />
                  <TableColumn prefWidth="278.0" text="Win Percentage" />
              </columns>
            </TableView>
            <TextField id="textTeamName" layoutX="117.0" layoutY="742.0" promptText="Team Name" />
            <Text layoutX="14.0" layoutY="768.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Team" />
            <Text layoutX="14.0" layoutY="838.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Wins" wrappingWidth="68.4609375" />
            <Text layoutX="14.0" layoutY="900.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Losses" />
            <TextField id="textWins" layoutX="117.0" layoutY="804.0" promptText="Wins (number)" />
            <TextField id="textLosses" layoutX="117.0" layoutY="866.0" promptText="Losses (number)" />
            <Button fx:id="enterTeamInfoButton" layoutX="477.0" layoutY="752.0" mnemonicParsing="false" text="Enter" />
            <Button fx:id="deleteTeamInfoButton" layoutX="470.0" layoutY="855.0" mnemonicParsing="false" text="Delete" />
            </children>
        </Pane>
    </children>
</GridPane>

I am programming a JavaFX program with Scene builder and FXML. I am trying to get it so when I press file new... it changes scenes. Currently, when I run the code, I get a NullPointerException on the line where Ienter code here type window1.setScene(new Scene(file)); I have determined that somehow my window1 reference is null because any method I try with window1 I get a NullPointerException.

Any suggestions?

Thank you so much!

Erresen
  • 1,923
  • 1
  • 22
  • 41
moisu
  • 343
  • 2
  • 10
  • Pretty easy: Assign a non-`null` value to your field, see [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – fabian May 08 '16 at 19:16
  • Why are you using the `Application` class as the controller? Create a controller class for each FXML file, distinct from the `Application` class. – James_D May 08 '16 at 19:48
  • I am having difficulty figuring out how to avoid making my Stage variable a null value. Does anyone know how to do that? – moisu May 08 '16 at 21:05

0 Answers0