ColonelSpuds ColonelSpuds - 5 months ago 8
Java Question

Java - Why is my static variable being set to null?

EDIT 1: I think I may have figured it out.

Here is my initialize() method

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


I think that when I reload the groupList ListView this selectedItemProperty listener is being called and setting it to null.

I have fixed this by adding an if condition inside the listener:

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





For some reason my static 'selectedGroup' variable is being set to null and I cannot figure out why.

I have carried out the following troubleshooting.

I have a method called 'refresh()' which calls a method called 'createGroupFilter()'. I have included the following System.out.println statements in order to see when the variable is getting nulled.

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;
}


This provides the following output:

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


So it appears to me that the groupList.setItems()... is what is causing the issue.

For reference, here is the MainScreenModel.getGroups() method:

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;
}


And also my Group class:

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

}

Answer

I cannot see anywhere that you call setSelectedGroup in your code. Since selectedGroup is default initialized to null, it is going to remain null if you don't call setSelectedGroup.

On the other hand, if (as it appears) selectedGroup is non-null at certain points, then some code that you haven't shown us MUST be calling the method. A Java variable won't change its value spontaneously.

So ....


Why is my static variable being set to null?

Something must be calling setSelectedGroup somewhere with a null parameter.

So it appears to me that the groupList.setItems()... is what is causing the issue.

That is plausible. It is also possible that something else is causing it; e.g. another thread.