【问题标题】:Java - Why is my static variable being set to null?Java - 为什么我的静态变量被设置为空?
【发布时间】:2016-06-13 13:18:23
【问题描述】:

编辑 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();
        }
    });

由于某种原因,我的静态“selectedGroup”变量被设置为 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();
    }

}

【问题讨论】:

  • setSelectedGroup() 方法是否曾将其设置为 null?
  • 我看不到你设置它的任何地方,还是我遗漏了什么?
  • 我也是这么说的,你在哪里填static Group selectedGroup;
  • 我没有看到 groupList 的声明。它是什么类型的?如果它是自定义的,我们可以获取 setItems() 的源代码

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


【解决方案1】:

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

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

所以....


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

必须在某处使用null 参数调用setSelectedGroup

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

这是有道理的。也有可能是其他原因造成的;例如另一个线程。


1 - 这就是问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-29
    • 2020-01-08
    • 2020-06-12
    • 2011-10-24
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多