当前位置: 首页 > 知识库问答 >
问题:

在java fx中尝试向另一个阶段发送和修改数据时出现空指针异常

郜德容
2023-03-14

嗨,我是java新手,我正在尝试制作一个游戏,所以我制作了一个单元格类和一个网格类来显示网格。u可以成功地将星星、墙或笑脸添加到网格中。但是,当我试图将网格发送到实际的游戏页面供玩家玩时,我得到了空指针异常,好像网格中没有用户输入笑脸的单元格,等等。我已经解决这个问题好几天了。请帮助请注意,入口鼠标手势类和游戏鼠标手势是相同的,因为我试图解决这个问题,然后详细工作

这是细胞类

package sample.CellandGrid;

import javafx.scene.layout.StackPane;

public class cell extends StackPane {

    int column;
    int row;
    boolean star;
    boolean wall;
    String player;

    public cell(int column, int row,boolean star,boolean wall,String player) {

        this.column = column;
        this.row = row;
        this.star=star;
        this.wall = wall;
        this.player= player;

        getStyleClass().add("cell");

//          Label label = new Label(this.toString());
//
//          getChildren().add(label);

        setOpacity(0.9);
    }
    public void makeStar(){
        getStyleClass().remove("cell-highlight");
        getStyleClass().add("cell-star-highlight");
    }
    public void removeStar(){
        getStyleClass().remove("cell-star-highlight");
        getStyleClass().add("cell");
    }
    public void makewall(){
        getStyleClass().remove("cell-highlight");
        getStyleClass().add("cell-wall-highlight");
    }
    public void removeWall(){
        getStyleClass().remove("cell-wall-highlight");
        getStyleClass().add("cell");
    }
    public void smileyone(){
        getStyleClass().remove("cell");
        getStyleClass().add("cell-smiley1-highlight");
    }
    public void removeSmileyone(){
        getStyleClass().remove("cell-smiley1-highlight");
        getStyleClass().add("cell");
    }
    public void smileytwo(){
        getStyleClass().remove("cell");
        getStyleClass().add("cell-smiley2-highlight");
    }
    public void removeSmileytwo(){
        getStyleClass().remove("cell-smiley2-highlight");
        getStyleClass().add("cell");
    }


    public void highlight() {
        // ensure the style is only once in the style list
        getStyleClass().remove("cell-highlight");

        // add style
        getStyleClass().add("cell-highlight");
    }

    public void unhighlight() {
        getStyleClass().remove("cell-star-highlight");
    }

    public void hoverHighlight() {
        // ensure the style is only once in the style list
        getStyleClass().remove("cell-hover-highlight");

        // add style
        getStyleClass().add("cell-hover-highlight");
    }

    public void hoverUnhighlight() {
        getStyleClass().remove("cell-hover-highlight");
    }

    public String toString() {
        return this.column + "/" + this.row;
    }
}

这是网格类

package sample.CellandGrid;

import javafx.scene.layout.Pane;

public class grid extends Pane {

    int rows;
    int columns;

    double width;
    double height;

    boolean star;
    boolean wall;

    cell [][] cells;

    public grid( int columns, int rows, double width, double height,boolean star,boolean wall) {

        this.columns = columns;
        this.rows = rows;
        this.width = width;
        this.height = height;
        this.star = star;
        this.wall = wall;

        cells = new cell[rows][columns];

    }

    /**
     * Add cell to array and to the UI.
     */
    public void add(cell cell, int column, int row) {

        cells[row][column] = cell;

        double w = width / columns;
        double h = height / rows;
        double x = w * column;
        double y = h * row;

        cell.setLayoutX(x);
        cell.setLayoutY(y);
        cell.setPrefWidth(w);
        cell.setPrefHeight(h);

        getChildren().add(cell);

    }

    public cell getCell(int column, int row) {
        return cells[row][column];
    }

    /**
     * Unhighlight all cells
     */
    public void unhighlight() {
        for( int row=0; row < rows; row++) {
            for( int col=0; col < columns; col++) {
                cells[row][col].unhighlight();
            }
        }
    }
}

这是入口鼠标手势类

package sample.CellandGrid;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import sample.UserEnteredGrid.userGrid;

public class entryMouseGesture {
        public void Paint( Node node) {


            // that's all there is needed for hovering, the other code is just for painting
            if( true) {
                node.hoverProperty().addListener(new ChangeListener<Boolean>(){

                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                        System.out.println( observable + ": " + newValue);

                        if( newValue) {
                            ((cell) node).hoverHighlight();
                        } else {
                            ((cell) node).hoverUnhighlight();
                        }

                        for( String s: node.getStyleClass())
                            System.out.println( node + ":gooz " + s);
                    }

                });
            }

            node.setOnMousePressed( onMousePressedEventHandler);


        }

        EventHandler<MouseEvent> onMousePressedEventHandler = event -> {

            PickResult pick = event.getPickResult();
            Node node = pick.getIntersectedNode();
            outerloop :
            if (node instanceof cell) {
                cell cell = (cell) event.getSource();
                if(userGrid.button.compareTo("star")==0) {
                    if (event.isPrimaryButtonDown()) {
                        cell.makeStar();
                        cell.star=true;
                        System.out.println("check"+cell.star);
                    } else if (event.isSecondaryButtonDown()) {
                        cell.removeStar();
                        cell.star=false;
                        System.out.println("check"+cell.star);

                    }
                } else if(userGrid.button.compareTo("smiley1")==0) {

                    if (event.isPrimaryButtonDown()) {

                        if(userGrid.hasFirstPlayer==true){
                            Alert alert = new Alert(Alert.AlertType.WARNING);
                            //alert.setAlertType(Alert.AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("ILLEGAL ACTION");
                            alert.setContentText("YOU HAVE PLACED THE FIRST PLAYER ALREADY!");
                            alert.showAndWait();
                            break outerloop;
                        }

                        cell.smileyone();
                        cell.player = "1";
                        userGrid.hasFirstPlayer=true;

                    } else if (event.isSecondaryButtonDown()) {
                        if(cell.player.compareTo("1")==0){
                            userGrid.hasFirstPlayer=false;
                        }
                        cell.removeSmileyone();
                        cell.player = "";
                        System.out.println("s1check");
                    }

                }else if(userGrid.button.compareTo("smiley2")==0) {
                    if (event.isPrimaryButtonDown()) {
                        if(userGrid.hasSecondPlayer==true){
                            Alert alert = new Alert(Alert.AlertType.WARNING);
                            //alert.setAlertType(Alert.AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("ILLEGAL ACTION");
                            alert.setContentText("YOU HAVE PLACED THE Second PLAYER ALREADY!");
                            alert.showAndWait();
                            break outerloop;
                        }
                        cell.smileytwo();
                        cell.player = "2";
                        userGrid.hasSecondPlayer=true;
                    } else if (event.isSecondaryButtonDown()) {
                        if(cell.player.compareTo("2")==0){
                            userGrid.hasSecondPlayer=false;
                        }
                        cell.removeSmileytwo();
                        cell.player = "";
                        System.out.println("s2check");

                    }
                }else if(userGrid.button.compareTo("wall")==0) {
                    if (event.isPrimaryButtonDown()) {
                        cell.makewall();
                        cell.wall=true;
                    } else if (event.isSecondaryButtonDown()) {
                        cell.removeWall();
                        cell.wall=false;
                        System.out.println("wall: "+cell.wall);

                    }
                }
            }

        };
        EventHandler<MouseEvent> onMouseReleasedEventHandler = event -> {
        };

}

这是游戏鼠标手势类

package sample.CellandGrid;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import sample.UserEnteredGrid.userGrid;

public class gameMouseGesture {

    public void Paint( Node node) {


         //that's all there is needed for hovering, the other code is just for painting
        if( true) {
            node.hoverProperty().addListener(new ChangeListener<Boolean>(){

                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                    System.out.println( observable + ": " + newValue);

                    if( newValue) {
                        ((cell) node).hoverHighlight();
                    } else {
                        ((cell) node).hoverUnhighlight();
                    }

                    for( String s: node.getStyleClass())
                        System.out.println( node + ":gooz " + s);
                }

            });
        }
        if (node instanceof cell){
            cell mycell =(cell) node;
            if (mycell.star==true){
                mycell.makeStar();
            }else if (mycell.wall==true){
                mycell.makewall();
            }else if (mycell.player=="1"){
                mycell.smileyone();
            }else if(mycell.player=="2"){
                mycell.smileytwo();
            }


        }



        node.setOnMousePressed( onMousePressedEventHandler);


    }

    EventHandler<MouseEvent> onMousePressedEventHandler = event -> {

        PickResult pick = event.getPickResult();
        Node node = pick.getIntersectedNode();
        outerloop :
        if (node instanceof cell) {
            cell cell = (cell) event.getSource();
            System.out.println("checked");
        }

    };
    EventHandler<MouseEvent> onMouseReleasedEventHandler = event -> {
    };

}

这是维度页面,用户在其中输入高度和宽度、行数等维度页面控制器

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.UserEnteredGrid.userGrid;

import java.io.IOException;



public class dimensionPageController {
    @FXML
    TextField GridWidth;
    @FXML
    TextField GridHeight;
    @FXML
    TextField row;
    @FXML
    TextField column;
    @FXML
    TextField name1;
    @FXML
    TextField name2;

    Parent root;
    Stage stage;

    //public static

    public void pageDimensions() throws IOException {
        int gHeight =Integer.parseInt(GridHeight.getText());
        int gWidth = Integer.parseInt(GridHeight.getText());
        int rrow = Integer.parseInt(row.getText());
        int ccolumn = Integer.parseInt(column.getText());
        dataModel.gridHeight=gHeight;
        dataModel.gridWidth=gWidth;
        dataModel.row=rrow;
        dataModel.column=ccolumn;
        //try {
        System.out.println("Dimension Happened");
        //System.out.println( gHeight);
        System.out.println("AIMING FOR GRID");
        userGrid grid = new userGrid();
        grid.start((Stage)row.getScene().getWindow());

//            try {
//                root = FXMLLoader.load(getClass().getResource("/sample/GridInitializer.fxml"));
//                stage = (Stage) row.getScene().getWindow();
//                Scene scene = new Scene(root, DataModel.gridWidth, DataModel.gridHeight);
//                stage.setScene(scene);
//                stage.setTitle("please god ");
//                stage.show();
//            } catch (Exception e) {
//
//                System.out.println("grid initiakizer went wring /init.page controller");
//                e.printStackTrace();
//            }
//        }catch (Exception ex){
//            System.out.println("101");
//            ex.getCause();
//            System.out.println("101");
//        }
//
    }
}

维度页面fxml文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane alignment="TOP_CENTER" hgap="10" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: rgb(38,38,38)"
          vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.dimensionPageController">

    <Label alignment="TOP_CENTER" style="-fx-text-fill: rgb(255,255,255);         -fx-font-size: 20;-fx-border-color: rgb(250,3,3);-fx-border-radius: 10" text=" FirstPageInitialization " GridPane.columnIndex="2" GridPane.rowIndex="1" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Page Width: " GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <TextField fx:id="GridWidth" GridPane.columnIndex="3" GridPane.rowIndex="2" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Page Height: " GridPane.columnIndex="1"
           GridPane.columnSpan="2" GridPane.rowIndex="3" />
    <TextField fx:id="GridHeight" GridPane.columnIndex="3" GridPane.rowIndex="3"  />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="First player Name: " GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="4" />
    <TextField fx:id="Name1" GridPane.columnIndex="3" GridPane.rowIndex="4" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Second player Name: " GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="5" />
    <TextField fx:id="Name2" GridPane.columnIndex="3" GridPane.rowIndex="5" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="rows: " GridPane.columnIndex="1" GridPane.rowIndex="6" />
    <TextField fx:id="row" GridPane.columnIndex="3" GridPane.rowIndex="6" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="columns: " GridPane.columnIndex="1" GridPane.rowIndex="7" />
    <TextField fx:id="column" GridPane.columnIndex="3" GridPane.rowIndex="7" />

    <Button fx:id="ok" alignment="CENTER" onMouseClicked="#pageDimensions" prefHeight="31.0" prefWidth="187.0" style="-fx-fill: rgb(255,255,255)" text="Save" GridPane.columnIndex="3" GridPane.columnSpan="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="8" />
    <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

</GridPane>

这是用户看到网格并将图标放在网格上的部分(svg路径)usergrid类:

package sample.UserEnteredGrid;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.CellandGrid.entryMouseGesture;
import sample.gamePageGrid.gamePageGRID;


public class userGrid extends Application {
    int rows = dataModel.row;
    int columns = dataModel.column;
    double width = dataModel.gridWidth;
    double height = dataModel.gridHeight;

    public static String button = "";
    public static boolean hasFirstPlayer = false;
    public static boolean hasSecondPlayer=false;


    static cell savedGridCells [][];

    public static grid TransitGrid;
    grid grid = new grid( columns, rows, width, height,false,false);
    @FXML
    Button starButton;

    HBox joinedRoot;

    public void start(Stage primaryStage) {
        try {
            StackPane root = new StackPane();
            Parent root2;
            // create grid


             entryMouseGesture mg = new entryMouseGesture();

            // fill grid
            for (int row = 0; row < rows; row++) {
                for (int column = 0; column < columns; column++) {

                    cell cell = new cell(column, row,false,false,"");

                    mg.Paint(cell);
                    //dataModel.datamodelCell[row][column]=cell;
                    grid.add(cell,column,row);

                }
            }

            root.getChildren().addAll(grid);
            root.setStyle("-fx-background-color: rgb(38,38,38)");
            root2= FXMLLoader.load(getClass().getResource("userGrid.fxml"));
            // create scene and stage
            joinedRoot = new HBox(root2,root);
            joinedRoot.setSpacing(10);
            System.out.println(root2.getLayoutX());
            joinedRoot.setStyle("-fx-background-color: rgb(38,38,38)");
            Scene scene2 = new Scene(new VBox(root2,root) , width, height+110);
            scene2.getStylesheets().addAll(getClass().getResource("userGrid.css").toExternalForm());

            primaryStage.setScene(scene2);
            primaryStage.show();



        } catch (Exception e) {
            System.out.println("tap");
            e.getCause();
            e.printStackTrace();
        }
    }

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

    @FXML
    public void star(){
        System.out.println("starshit");
        button="star";
    }
    @FXML
    public void smiley1(){
        System.out.println("s1shit");
        button="smiley1";
    }
    @FXML
    public void smiley2(){
        System.out.println("s2shit");
        button="smiley2";
    }
    @FXML
    public void wall(){
        System.out.println("wall shit");
        button="wall";
    }

    @FXML
    private BorderPane gamePageBorderPane;

    @FXML
    public void submit(){
        dataModel.dataModelGrid=grid;
        System.out.println("first check "+grid.getCell(1,1));
        System.out.println("check "+dataModel.dataModelGrid.getCell(1,1));
        System.out.println("game page happening");
         gamePageGRID gamePage = new gamePageGRID();
         gamePage.start(new Stage());

//        try {
//            Stage stagew;
//            Parent root;
//            stagew = (Stage) starButton.getScene().getWindow();
//            root = FXMLLoader.load(getClass().getResource("gamePage.fxml"));
//            Scene scene = new Scene(root);
//            stagew.setScene(scene);
//            stagew.setTitle("GAME WINDOW");
//
//            stagew.show();
//        }catch (Exception e){
//            System.out.println("ude");
//            e.getCause();
//            System.out.println("ude");
//            e.printStackTrace();
//        }

    }


}

用户网格css

.cell{
    -fx-background-color: rgb(38,38,38);
    -fx-border-color: red;
    -fx-border-width: 1px;
}
.cell-highlight {
    -fx-background-color:derive(blue,0.9);
}
.cell-hover-highlight {
    -fx-background-color:derive(green,0.9);
}
.cell-star-highlight{

    /*-fx-background-color: derive(white,1%);*/
    /*-fx-shape:"M2,2 L8,2 L2,5 L8,5 L2,8 L8,8";*/

    -fx-shape: "M 100 0 L175 200 L0 75 L200 75 L25 200 Z" ;
    -fx-fill: derive(yellow,10%);
    -fx-background-color: derive(yellow,10%);

}
.cell-smiley1-highlight{
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: derive(red,10%);
    -fx-stroke: blue;
    -fx-stroke-width: 5
}
.cell-smiley2-highlight{
    -fx-background-color: derive(blue,10%);
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: rgb(255,255,255);
    -fx-stroke: red;
    -fx-stroke-width: 5
}
.cell-wall-highlight{
    -fx-background-color: derive(white,1%);
    -fx-shape:"M2,2 L8,2 L2,5 L8,5 L2,8 L8,8";
}
.background{
    -fx-background-color: rgb(38,38,38);
}

用户网格 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!--<?import sample.userGrid?>-->
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>


<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<GridPane hgap="5" stylesheets="@userGrid.css"
          xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.UserEnteredGrid.userGrid">
    <Label text="click on button Left-click to put the element Right-click to remove" GridPane.rowIndex="1"/>
    <HBox spacing="10" GridPane.rowIndex="2">
        <Button fx:id="starButton" id="starButton-highlight" onMouseClicked="#star" text="Star" ></Button>
        <Button fx:id="wallButton" id="wallButton-highlight" onMouseClicked="#wall" text="Wall" ></Button>
        <Button fx:id="smiley1Button" id="smiley1Button-highlight" onMouseClicked="#smiley1" text="smiley1" ></Button>
        <Button fx:id="smiley2Button" id="smiley2Button-highlight" onMouseClicked="#smiley2" text="smiley2" ></Button>
        <Button fx:id="submit" text="SUBMIT" onMouseClicked="#submit"/>
    </HBox>


</GridPane>

这是游戏页面,用户可以在其中玩gamepage类

package sample.gamePageGrid;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import sample.CellandGrid.gameMouseGesture;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.UserEnteredGrid.userGrid;


// import static sample.dimensionPageController.grid;


public class gamePageGRID extends Application{

    int rows = dataModel.row;
    int columns = dataModel.column;
    double width = dataModel.gridWidth;
    double height = dataModel.gridHeight;

    public static String button = "";
    public static boolean hasFirstPlayer = false;
    public static boolean hasSecondPlayer=false;


    static cell savedGridCells [][];

    static grid TransitGrid;
    grid mygrid = new grid( columns, rows, width, height,false,false);
    @FXML
    Button starButton;

    HBox joinedRoot;

    public void start(Stage primaryStage) {

        StackPane ggridRoot =new StackPane();

        gameMouseGesture mg = new gameMouseGesture();

        System.out.println("OKAY GAME PAGE");

        for (int row=0;row<rows;row++){
            for (int column=0;column<columns;columns++){
                cell mycell=(cell) dataModel.dataModelGrid.getCell(column,row);
                mg.Paint(mycell);
                mygrid.add(mycell,column,row);

            }
        }
        ggridRoot.getChildren().addAll(mygrid);
        Scene gameScene = new Scene(ggridRoot);
        gameScene.getStylesheets().addAll(getClass().getResource("gameGrid.css").toExternalForm());
        primaryStage.setScene(gameScene);
        primaryStage.show();
    }

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

这是数据模型

package sample.Data;
import sample.CellandGrid.*;
public class dataModel {
    public static int gridHeight;
    public static int gridWidth;
    public static String name1;
    public static String name2;
    public static int row;
    public static int column;
    public static grid dataModelGrid;

}

下面是错误的堆栈跟踪

Caused by: java.lang.NullPointerException
    at sample.CellandGrid.gameMouseGesture.Paint(gameMouseGesture.java:19)
    at sample.gamePageGrid.gamePageGRID.start(gamePageGRID.java:57)
    at sample.UserEnteredGrid.userGrid.submit(userGrid.java:123)
    ... 41 more

我的项目结构是这样的

共有2个答案

仲孙向明
2023-03-14
cell cell = new cell(column, row, false ,false, "");

类型和对象具有相同的名称,这对我来说似乎是一个非常糟糕的主意。将类命名为“Cell”,将对象命名为“cell”,并告诉我它是否运行得更好。

Cell cell = new Cell(column, row, false ,false, "");

类的声明变成:

public class Cell extends StackPane {
}

它的构造函数变成了:

public Cell(int column, int row, boolean star, boolean wall, String player) {
}

按照惯例,所有类名应以大写字母开头,所有对象应以小写字母开头。

阎星河
2023-03-14

好了,我终于找到了答案< br >我向任何花时间回顾我的问题的人道歉,因为这个问题完全是由我的大量无知和我没有遵循Java命名约定造成的,没有重要的学习价值< br>
这个问题是由游戏页面中的第二个for循环引起的,在这个循环中,我添加了边界而不是变量 让这成为始终遵循JAVA命名约定的一个好例子,否则您将花费数天时间试图在一堆干草中找一根针**
我已经纠正了代码

  for (int row=0;row<rows;row++){
            for (int column=0;column<columns;column++){
                cell mycell=(cell) dataModel.dataModelGrid.getCell(column,row);
                mg.Paint(mycell);
                mygrid.add(mycell,column,row);

            }
        }
 类似资料:
  • 我有三层根布局、主页和内容(rootlayout.fxml、home.fxml和content.fxml) 像这样,我正在添加内容布局。在rootlayout.fxml中,我有一个home按钮。我的要求是,如果一个用户点击主页按钮,那么我希望内容布局被删除,主页布局是可见的。 在我创建的Controller(在我指向同一个Controller的所有fxml文件中)类中 我面临的问题是我得到了Nul

  • 我得到的只有java.lang.NullPointerException。 我还尝试了各种命名风格,如java:jdbc/mydb或java:comp/env/jdbc/mydb 这使用了最新的Glassfish(4.1),针对Postgres 9.4的最新Postgres驱动程序(9.3-1102 JDBC 41),以及最新的Java(1.8.0_31-B13)。

  • zapier中的Javascript代码 在Action类中,我正在获取请求数据 正在获取异常 java.lang.NullPointerException\n\tat java.io.StringReader

  • 在服务器日志中可以找到如下所示的多个错误 com.sun.faces.lifecycle.RenderResponsePhase.Execute处的java.lang.NullPointerException(RenderResponsePhase.java:95) 如果我检查了JSF的源代码,问题就在这行源代码上 ViewDeclarationLanguage vdl=VH.GetViewDec

  • 以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena

  • 我一直在构建一个影院预订应用程序,正在尝试创建一个显示电影和放映时间的场景。当我使用锚点窗格和vbox来显示所有信息时,它起作用,但当我尝试插入一个额外的滚动窗格(在scenebuilder中)时,FXML加载程序返回一个空指针异常,我无法找出原因... 这是我的FXML代码 下面是controller类 编辑:fx:id'vbox'不会从getRoot()方法访问,即使它已被注入到FXML加载器