在Java和JavaFX中创建二维ArrayList时发生NullPointerException

时间:2018-12-08 10:45:52

标签: java arrays javafx dynamic-arrays

我想用JavaFX编写“四胜”游戏,但是当声明二维TextField-Array“字段”并且我想打印出列表项时,我得到了NullPointerException,因为没有TextField-Objects,但只能是null-Objects。有人可以帮我吗?

@FXML Button button1, button2, button3, button4, button5, button6, button7;
    @FXML TextField field00, field01, field02, field03, field04, field05,
              field10, field11, field12, field13, field14, field15,
              field20, field21, field22, field23, field24, field25,
              field30, field31, field32, field33, field34, field35,
              field40, field41, field42, field43, field44, field45,
              field50, field51, field52, field53, field54, field55,
              field60, field61, field62, field63, field64, field65;

    @FXML TextField[][] fields = {
            {field00, field01, field02, field03, field04, field05},
            {field10, field11, field12, field13, field14, field15},
            {field20, field21, field22, field23, field24, field25},
            {field30, field31, field32, field33, field34, field35},
            {field40, field41, field42, field43, field44, field45},
            {field50, field51, field52, field53, field54, field55},
            {field60, field61, field62, field63, field64, field65}
    };

    ArrayList<ArrayList<TextField>> freeFields = new ArrayList<>();

    private String player;
    private int turn = 1;

    public void initialize(){
        player();

        System.out.println(field00.getClass());
        for(int i = 0; i < fields.length; i++){
            ArrayList<TextField> arrayList = new ArrayList<>();
            for(int j = 0; j < fields[0].length; j++){
                arrayList.add(fields[i][j]);
            }
            freeFields.add(arrayList);
        }

    }

1 个答案:

答案 0 :(得分:0)

的右侧
TextField[][] fields = {
        {field00, field01, field02, field03, field04, field05},
        {field10, field11, field12, field13, field14, field15},
        {field20, field21, field22, field23, field24, field25},
        {field30, field31, field32, field33, field34, field35},
        {field40, field41, field42, field43, field44, field45},
        {field50, field51, field52, field53, field54, field55},
        {field60, field61, field62, field63, field64, field65}
};

是在对象创建期间(在初始化程序中)求值的。

目前,FXMLLoader尚未插入任何字段。如果要解决此问题,请将字段的初始化移至initialize方法。插入所有字段后,此方法由FXMLLoader运行:

private TextField[][] fields;

@FXML
private void initialize() {
    fields = ...;
}

我认为使用SceneBuilder / fxml设计这种GUI不是一个好主意。在fxml中,您有42 = 7 * 6 TextField个对象,如果仅在一个fx:id中有错字,则会破坏代码。如果您使用循环创建这些TextField并将其添加到场景和数据结构中,则将更好/更容易维护

@FXML
private GridPane container; // inject a parent for TextFields here

private TextField[][] fields;

@FXML
private void initialize() {
    fields = new TextField[7][6];
    for (int row = 0; row < fields.length; row++) {
        TextField[] fs = fields[row];
        for (int col = 0; col < fs.length; col++) {
            TextField f = new TextField();
            fs[col] = f;
            container.add(f, col, row);
        }
    }
}