Java - 为什么我的静态变量被设置为null?

时间:2016-06-13 13:18:24

标签: java javafx static java-8 javafx-8

编辑1:我想我可能已经弄明白了。

这是我的initialize()方法

public void initialize(URL location, ResourceBundle resources) {
    groupList.setItems(createGroupFilter());
    groupList.getSelectionModel().selectedItemProperty().addListener(obs -> {
        Group.setSelectedGroup(groupList.getSelectionModel().getSelectedItem());
        handleGroupSelected();
    });

我认为当我重新加载groupList ListView时,正在调用这个selectedItemProperty侦听器并将其设置为null。

我已通过在侦听器中添加if条件来解决此问题:

public void initialize(URL location, ResourceBundle resources) {
    groupList.setItems(createGroupFilter());
    groupList.getSelectionModel().selectedItemProperty().addListener(obs -> {
        if(groupList.getSelectionModel().getSelectedItem() != null) {
            Group.setSelectedGroup(groupList.getSelectionModel().getSelectedItem());
            handleGroupSelected();
        }
    });

出于某种原因,我的静态选择组'变量被设置为null,我无法弄清楚原因。

我已经进行了以下故障排除。

我有一个名为' refresh()'它调用了一个名为' createGroupFilter()'的方法。我已经包含了以下System.out.println语句,以便查看变量何时被置为空。

private void refresh() {
    System.out.println("before groupList.setItems(createGroupFilter()); "  + Group.getSelectedGroup()); //TROUBLESHOOTING
    groupList.setItems(createGroupFilter());
    System.out.println("after groupList.setItems(createGroupFilter()); "  + Group.getSelectedGroup()); //TROUBLESHOOTING
    if(Group.getSelectedGroup() != null) {
        groupList.getSelectionModel().select(Group.getSelectedGroup());
        int n = groupList.getSelectionModel().getSelectedIndex();
        groupList.getFocusModel().focus(n);
        groupList.scrollTo(n);
        userList.setItems(createUserFilter());
        if(User.getSelectedUser() != null) {
            userList.getSelectionModel().select(User.getSelectedUser());
        }
    }
}

private FilteredList<Group> createGroupFilter() {
    System.out.println("start createGroupFilter(); " + Group.getSelectedGroup()); //TROUBLESHOOTING
    groups = MainScreenModel.getGroups();
    FilteredList<Group> filteredGroups = new FilteredList<Group>(groups, s -> true);
    groupSearchTxt.textProperty().addListener(obs -> {
        String filter = groupSearchTxt.getText().toLowerCase();
        if(filter == null || filter.trim().length() == 0) {
            filteredGroups.setPredicate(s -> true);
        } else {
            filteredGroups.setPredicate(s -> s.toString().toLowerCase().contains(filter));
        }
    });
    System.out.println("end createGroupFilter(); " + Group.getSelectedGroup()); //TROUBLESHOOTING
    return filteredGroups;
}

这提供了以下输出:

before groupList.setItems(createGroupFilter()); Group 1
start createGroupFilter(); Group 1
end createGroupFilter(); Group 1
after groupList.setItems(createGroupFilter()); null

所以在我看来,groupList.setItems()...是导致问题的原因。

作为参考,这是MainScreenModel.getGroups()方法:

public static ObservableList<Group> getGroups() {
    ObservableList<Group> groups = FXCollections.observableArrayList();
    try {
        PreparedStatement stmt = DatabaseConnection.getConnection()
                .prepareStatement("SELECT * FROM groups ORDER BY group_name ASC");
        ResultSet rs = stmt.executeQuery();
        while(rs.next()) {
            String gn = rs.getString("GROUP_NAME");
            String desc = rs.getString("DESCRIPTION");
            groups.add(new Group(gn, desc));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return groups;
}

还有我的小组课程:

public class Group {

    private static Group selectedGroup;

    private final StringProperty groupName   = new SimpleStringProperty();
    private final StringProperty description = new SimpleStringProperty();

    public Group(String hn, String desc) {
        setGroupName(hn);
        setDescription(desc);
    }

    public StringProperty hostNameProperty() {
        return this.groupName;
    }

    public String getGroupName() {
        return this.groupName.get();
    }

    public void setGroupName(String hn) {
        this.groupName.set(hn);
    }

    public StringProperty descriptionProperty() {
        return this.description;
    }

    public String getDescription() {
        return this.description.get();
    }

    public void setDescription(String desc) {
        this.description.set(desc);
    }

    public static Group getSelectedGroup() {
        return selectedGroup;
    }

    public static void setSelectedGroup(Group group) {
        selectedGroup = group;
    }

    @Override
    public String toString() {
        return this.groupName.get();
    }

}

1 个答案:

答案 0 :(得分:6)

我无法看到您在代码中调用setSelectedGroup的任何地方。由于selectedGroup默认初始化为null,因此如果您不致电null,它将保持setSelectedGroup

另一方面,如果(在它看来)selectedGroup在某些点上是非空的,那么你没有向我们展示的一些代码 1 必须调用方法。 Java变量不会自发地改变它的值。

所以......

  

为什么我的静态变量被设置为null?

某些内容必须使用setSelectedGroup参数调用null

  

所以在我看来,groupList.setItems()...是导致问题的原因。

这似乎是合情合理的。其他东西也可能导致它;例如另一个线程。

1 - 那就是问题。