【发布时间】:2019-10-31 04:18:22
【问题描述】:
所以我的问题是由于某种原因我的 AttributeSet 为空,我不确定如何解决这个问题。
public class MyDocumentFilter extends DocumentFilter {
//TODO FIX THIS, as IS NULL FOR SOME REASON
@Override
public void replace(FilterBypass fb, int i, int o, String s, AttributeSet set) throws BadLocationException{
for(int n = s.length(); n > 0; n--){
char c = s.charAt(n - 1);
System.out.println(set);
if(Character.isAlphabetic(c) || c == ' '){
super.replace(fb, i, o, String.valueOf(c), set);
}else{
JOptionPane.showMessageDialog(null, "Error: Type field must not contain numbers and should be longer than 1 character");
}
}
}
@Override
public void remove(FilterBypass fb, int i, int o) throws BadLocationException{
super.remove(fb, i, o);
}
public void insertString(FilterBypass fb, int i, String s, AttributeSet set) throws BadLocationException{
super.insertString(fb, i, s, set);
}
这是我的代码,我基于 stackoverflow 上的另一个答案。(jTextField accept only alphabet and white space)
前几天我可以发誓这可以正常工作,但当我今天给我的程序测试数据时,它开始给我 NullPointerExceptions。我完全不知道为什么我的 AttributeSet 为空,有人可以帮忙吗?
Edit1:好的,现在我不完全确定是我的 AttributeSet 为空。我也不确定 NPE 的来源是否是 .replace() 方法。在另一个类中,我初始化了所有的摆动组件,如果我删除 typeField.setText(null)(typeField 是 jTextArea 的名称),程序就不会再抛出 NPE。现在我更加困惑了。
【问题讨论】:
标签: java