代码之家  ›  专栏  ›  技术社区  ›  Alex Hendrik

向窗格添加ArrayList矩形时出现NullPointerException[重复]

  •  0
  • Alex Hendrik  · 技术社区  · 6 年前

    每当我尝试使用“simulationPane”向窗格添加填充有矩形的ArrayList时,我需要一些帮助,以解决一个神秘的NullPointerException问题。getChildren()。addAll(墙列表)“”。我用一组矩形做过类似的尝试,结果也一样。

    非常感谢您的帮助!

    public class SimulationManager extends Application{
    
    public static int currentTime;
    
    public int agentNumber = 0;
    public int initialFear = 0;
    
    public Pane simulationPane;
    
    
    public HBox controlPane;
    public GridPane gridPane;
    public BorderPane borderPane;
    
    public boolean isPaused = false;
    
    GenericAgent[] agentList;
    
    ArrayList<Rectangle> wallList = new ArrayList<Rectangle>();
    
    Rectangle wall1;
    Rectangle wall2;
    Rectangle wall3;
    Rectangle wall4;
    Rectangle wall5;
    
    public static void main(String[] args) {
    
    
    
        Application.launch( args);
    
    
    
    
    }
    
    
    
    public void createOneRoom() {
        Rectangle wall1 = new Rectangle(0, 0, 500, 20);
        Rectangle wall2 = new Rectangle(0, 500, 20, 500);
        Rectangle wall3 = new Rectangle(500, 500, 500, 20);
        Rectangle wall4 = new Rectangle(0, 0, 20, 200);
        Rectangle wall5 = new Rectangle(300, 0, 20, 200);
    
        wallList.add(wall1);
        wallList.add(wall2);
        wallList.add(wall3);
        wallList.add(wall4);
        wallList.add(wall5);
    
        System.out.print(wallList);
    
    }
    
    public void createTwoRooms() {
        //wallList = new Rectangle[13];
    
        Rectangle room1Wall1 = new Rectangle();
        Rectangle room1Wall2 = new Rectangle();
        Rectangle room1Wall3 = new Rectangle();
        Rectangle room1Wall4 = new Rectangle();
        Rectangle room1Wall5 = new Rectangle();
    
        Rectangle room2Wall1 = new Rectangle();
        Rectangle room2Wall2 = new Rectangle();
        Rectangle room2Wall3 = new Rectangle();
        Rectangle room2Wall4 = new Rectangle();
        Rectangle room2Wall5 = new Rectangle();
    
        Rectangle corridor1 = new Rectangle();
        Rectangle corridor2 = new Rectangle();
        Rectangle corridor3 = new Rectangle();
    
        wallList.add(room1Wall1);
        wallList.add(room1Wall2);
        wallList.add(room1Wall3);
        wallList.add(room1Wall4);
        wallList.add(room1Wall5);
    
        wallList.add(room2Wall1);
        wallList.add(room2Wall2);
        wallList.add(room2Wall3);
        wallList.add(room2Wall4);
        wallList.add(room2Wall5);
    
        wallList.add(corridor1);
        wallList.add(corridor2);
        wallList.add(corridor3);
    
    
    
    }
    
    public void createThreeRooms() {
    
    }
    
    public void createCornerRoom() {
    
    }
    
    
    
    public void startSimulation(int agentNumber, int initialFear, String level) {
    
        agentList = new GenericAgent[agentNumber];
    
        ArrayList<Rectangle> wallList = new ArrayList<Rectangle>();
    
        //wallList = new Rectangle[50];
    
        for (int i = 0; i < agentNumber; i++) {
    
            if (Math.random() < 0.5) {
                agentList[i] = new StandardAgent();
                agentList[i].setFear(initialFear);
            }
    
            else if(Math.random() < 0.75) {
                agentList[i] = new GentleAgent();
            }
    
            else {
                agentList[i] = new AggrAgent();
            }
        }
    
        if (level == "One room") {
            createOneRoom();
            System.out.println("A single room was created");
    
        } else if (level == "Two rooms") {
            createTwoRooms();
            System.out.println("Two rooms were created");
    
        } else if (level == "Three rooms") {
            createThreeRooms();
            System.out.println("Three rooms were created");
    
        } else if (level == "Corner room") {
            createCornerRoom();
            System.out.println("A single corner room was created");
    
        }
    
    
        simulationPane.getChildren().addAll(wallList);
    
        for (Rectangle wall : wallList) {
            wall.setFill(Color.DARKGREY);
        }
    
    
        System.out.println("Number of agents created: " + agentList.length);
    
    
    
    }
    
    
    
    
    public void resetSimulation() {
        agentNumber = 0;
        initialFear = 0;
        isPaused = false;
    
        //TODO Remove all agents and walls.
    }
    
    
    
    
    
    //****************************************************************GUI**********************************************************************************//
    
    
    
    
    
    
    @Override
    public void start(Stage primaryStage) {
    
        BorderPane borderPane = new BorderPane();
    
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
    
        Pane simulationPane = new Pane();
    
        simulationPane.setPrefSize(500, 500);
    
        HBox controlPane = new HBox();
    
    
    
    
    
        Scene menuScene = new Scene(borderPane);
        primaryStage.setTitle("Agent-Based Fear Simulation");
        primaryStage.setScene(menuScene);
        primaryStage.show();
    
        gridPane.setStyle("-fx-background-color: black;");
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setVgap(15);
        gridPane.setHgap(5);
    
        MenuBar menuBar = new MenuBar();
    
        Menu menuFile = new Menu("File");
    
        MenuItem miMenu = new MenuItem("Return to menu");
        MenuItem miSave = new MenuItem("Save");
        MenuItem miLoad = new MenuItem("Load");
        MenuItem miClose = new MenuItem("Close                   ");
    
        miMenu.setOnAction(e -> {
            borderPane.setCenter(gridPane);
            borderPane.setBottom(null);
            resetSimulation();
        });
        miClose.setOnAction(e -> {Platform.exit();});
        miSave.setOnAction(e -> {save();});
        miLoad.setOnAction(e -> {selectFile();});
    
        menuFile.getItems().addAll(miMenu, miSave, miLoad, miClose);
        menuBar.getMenus().add(menuFile);
    
        borderPane.setTop(menuBar);
        borderPane.setCenter(gridPane);
    
    
    
        Text agentText = new Text("Type in the number of agents:");
        agentText.setFill(Color.WHITE);
        gridPane.add(agentText, 0, 2);
    
        Text fearText = new Text("Type in the initial fear value:");
        fearText.setFill(Color.WHITE);
        gridPane.add(fearText, 0, 4);
    
        Text levelText = new Text("Select the environment:");
        levelText.setFill(Color.WHITE);
        gridPane.add(levelText, 0, 6);
    
        Button startButton = new Button("Start the simulation");
        gridPane.add(startButton, 1, 0);
    
        /*Button testButton = new Button("Test");
        simulationPane.getChildren().add(testButton);
    
        Rectangle testRectangle = new Rectangle(10,10,10,10);
        simulationPane.getChildren().add(testRectangle);*/
    
        Button pauseButton = new Button("Pause/Play");
        controlPane.getChildren().add(pauseButton);
    
        pauseButton.setOnAction(e -> {isPaused = !isPaused;});
    
        TextField agentNumberBox = new TextField();
        gridPane.add(agentNumberBox, 1, 2);
    
        TextField initialFearBox = new TextField();
        gridPane.add(initialFearBox, 1, 4);
    
        final ComboBox<String> levelBox = new ComboBox<String>();
        levelBox.getItems().addAll("One room", "Two rooms", "Three rooms", "Corner room");
        gridPane.add(levelBox, 1, 6);
        levelBox.getSelectionModel().selectFirst();
    
        /* As the user I want to be able to set the agent number
         * As the user I want to be able to set the initial fear values
         * As the user I want to be able to pick the simulation environment
         * As the user I want to have a menu with all of the main functions of the simulation
         */
    
        startButton.setOnAction(e -> {
            try {
            agentNumber = Integer.parseInt(agentNumberBox.getText());
            initialFear = Integer.parseInt(initialFearBox.getText());
            String level = levelBox.getValue();
            borderPane.setCenter(null);
            borderPane.setCenter(simulationPane);
            borderPane.setBottom(controlPane);
            startSimulation(agentNumber, initialFear, level);
    
    
            } catch(NumberFormatException error) {
    
                System.out.println("Error caught:  wrong data type used.");
    
                Alert alert = new Alert(AlertType.ERROR);
                alert.setTitle("Error Initializing");
                alert.setHeaderText("Oops, an error has been caught!");
                alert.setContentText("Please use integers for the number of agents and the initial fear value.");
    
                alert.showAndWait();
    
            }
    
    
        });
    
    
    
    
    
    
    
    }
    
    
    
    public void save() {
    
    }
    
    
    //  User Story:  As the user, I want to be able to select the file to load.
    
    public void selectFile() {
    
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Simulation File");
        fileChooser.getExtensionFilters().addAll(
                 new ExtensionFilter("Comma Separated Values", "*.csv"));
         File selectedFile = fileChooser.showOpenDialog(null);
         if (selectedFile != null) {
           loadFile(selectedFile);
         }
    }
    
    public void loadFile(File selectedFile) {
    
    }
    

    }

    1 回复  |  直到 6 年前
        1
  •  0
  •   JoschJava    6 年前

    您需要初始化窗格:

    public Pane simulationPane = new Pane();