!7 fix NPE for removed group

Merge pull request !7 from tangcent/fix/NPE
This commit is contained in:
makejava 2020-02-11 12:01:17 +08:00 committed by Gitee
commit a2c8648ed3
1 changed files with 22 additions and 0 deletions

View File

@ -151,6 +151,8 @@ public class TemplateSettingPanel implements Configurable {
// 创建主面板 // 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout()); JPanel mainPanel = new JPanel(new BorderLayout());
this.currGroupName = findExistedGroupName(this.currGroupName);
// 实例化分组面板 // 实例化分组面板
this.baseGroupPanel = new BaseGroupPanel(new ArrayList<>(group.keySet()), this.currGroupName) { this.baseGroupPanel = new BaseGroupPanel(new ArrayList<>(group.keySet()), this.currGroupName) {
@Override @Override
@ -254,6 +256,25 @@ public class TemplateSettingPanel implements Configurable {
return mainPanel; return mainPanel;
} }
/**
* 获取存在的分组名
*
* @param groupName 分组名
* @return 存在的分组名
*/
private String findExistedGroupName(String groupName) {
//如果groupName不存在
if (!group.containsKey(groupName)) {
if (group.containsKey(Settings.DEFAULT_NAME)) {//尝试使用默认分组
return Settings.DEFAULT_NAME;
} else {
//获取第一个分组
return group.keySet().stream().findFirst().orElse(Settings.DEFAULT_NAME);
}
}
return groupName;
}
/** /**
* 添加调试面板 * 添加调试面板
*/ */
@ -414,6 +435,7 @@ public class TemplateSettingPanel implements Configurable {
// 防止对象篡改需要进行克隆 // 防止对象篡改需要进行克隆
this.group = CloneUtils.cloneByJson(settings.getTemplateGroupMap(), new TypeReference<Map<String, TemplateGroup>>() {}); this.group = CloneUtils.cloneByJson(settings.getTemplateGroupMap(), new TypeReference<Map<String, TemplateGroup>>() {});
this.currGroupName = settings.getCurrTemplateGroupName(); this.currGroupName = settings.getCurrTemplateGroupName();
this.currGroupName = findExistedGroupName(settings.getCurrTemplateGroupName());
if (baseGroupPanel == null) { if (baseGroupPanel == null) {
return; return;
} }