【发布时间】:2013-06-05 20:45:32
【问题描述】:
如果我使用 HashMap,我的代码可以工作,但如果我使用 TreeMap,则不能, 任何人都可以告诉这是为什么?
这是我的代码:
package ka.fil;
import java.util.HashMap;
import java.util.Map;
public class ModelInMemory implements Model {
private Map<String,BeanRecord> map = new HashMap<>();
@Override
public void putRecord(BeanRecord beanRecord) {
map.put(beanRecord.getEmail(), beanRecord);
}
@Override
public BeanRecord getRecord(String email) {
BeanRecord r = map.get(email);
return r;
}
@Override
public Iterable<BeanRecord> allRecord() {
return map.values();
}
public ModelInMemory() {
}
}
我所说的不工作的意思是,当我在主要方法中使用它时,我得到了这个:
Exception in thread "main" java.lang.NullPointerException at
java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source)
at ka.fil.ModelInMemory.putRecord(ModelInMemory.java:11)
at ka.fil.AppBatch.main(AppBatch.java:10)
【问题讨论】:
-
你能定义不工作吗?
-
你能把你得到的错误信息贴出来吗?
-
按字典顺序排序,即。
-
@ZiyaoWei:当我在主要方法中使用它时,我得到了这个:
Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at ka.fil.ModelInMemory.putRecord(ModelInMemory.java:11) at ka.fil.AppBatch.main(AppBatch.java:10) -
如果您想了解更多有关 TreeMap 的信息,请参阅我的 Internal life of TreeMap 教程