我已经写了一个任务经理,而且很好;这是一个很长的故事......顺便说一下,所有这些都是用Java编写的。所以我写了一个Facade,你可以在下面看到HashMap存在问题,我怀疑在构建过程中我试图添加到HashMap中的值并不是那么好。触发空指针异常的方法是create方法。我已经验证了该方法的输入参数,并且我的可信调试器已被填充。
这里的任何帮助都会很棒......我确定我忘了提一些东西所以我会尽快回复评论,因为我现在需要完成这件事。
package persistence;
import java.util.UUID;
import java.util.HashMap;
import persistence.framework.ComplexTaskRDBMapper;
import persistence.framework.IMapper;
import persistence.framework.RepeatingTaskRDBMapper;
import persistence.framework.SingleTaskRDBMapper;
public class PersistanceFacade {
@SuppressWarnings("unchecked")
private static Class SingleTask;
@SuppressWarnings("unchecked")
private static Class RepeatingTask;
@SuppressWarnings("unchecked")
private static Class ComplexTask;
private static PersistanceFacade uniqueInstance = null;
@SuppressWarnings("unchecked")
private HashMap<Class, IMapper> mappers;
public PersistanceFacade() {
mappers = new HashMap<Class, IMapper>();
try {
SingleTask = Class.forName("SingleTask");
RepeatingTask = Class.forName("RepeatingTask");
ComplexTask = Class.forName("ComplexTask");
mappers.put(SingleTask, new SingleTaskRDBMapper());
mappers.put(RepeatingTask, new RepeatingTaskRDBMapper());
mappers.put(ComplexTask, new ComplexTaskRDBMapper());
}
catch (ClassNotFoundException e) {}
}
public static synchronized PersistanceFacade getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new PersistanceFacade();
return uniqueInstance;
}
else return uniqueInstance;
}
public void create(UUID oid, Object obj) {
IMapper mapper = (IMapper) mappers.get(obj.getClass());
mapper.create(oid, obj);
}
@SuppressWarnings("unchecked")
public Object read(UUID oid, Class type) {
IMapper mapper = (IMapper) mappers.get(type);
return mapper.read(oid);
}
public void update(UUID oid, Object obj) {
IMapper mapper = (IMapper) mappers.get(obj.getClass());
mapper.update(oid, obj);
}
@SuppressWarnings("unchecked")
public void destroy(UUID oid, Class type) {
IMapper mapper = (IMapper) mappers.get(type);
mapper.destroy(oid);
}
}
答案 0 :(得分:2)
要让Class.forName("RepeatingTask")
返回课程,您必须拥有课程persistence.RepeatingTask
。但是在您的评论中,您说obj.getClass()
会返回domain.RepeatingTask
,因此我认为您有2个“RepeatingTask”类或domain.RepeatingTask
是子类型。
答案 1 :(得分:1)
我的猜测是你的问题在于构造函数:
try {
SingleTask = Class.forName("SingleTask");
RepeatingTask = Class.forName("RepeatingTask");
ComplexTask = Class.forName("ComplexTask");
mappers.put(SingleTask, new SingleTaskRDBMapper());
mappers.put(RepeatingTask, new RepeatingTaskRDBMapper());
mappers.put(ComplexTask, new ComplexTaskRDBMapper());
}
catch (ClassNotFoundException e) {}
您默默忽略ClassNotFOundException
。如果你向catch添加日志记录我希望它告诉你找不到类SingleTask
,因为我希望你没有把这些类放在默认包中。
鉴于您对评论的回复,这些课程都在domain.
包中,因此您可以尝试更改为:
try {
SingleTask = Class.forName("domain.SingleTask");
RepeatingTask = Class.forName("domain.RepeatingTask");
ComplexTask = Class.forName("domain.ComplexTask");
mappers.put(SingleTask, new SingleTaskRDBMapper());
mappers.put(RepeatingTask, new RepeatingTaskRDBMapper());
mappers.put(ComplexTask, new ComplexTaskRDBMapper());
}
catch (ClassNotFoundException e) {
log.warn("Cannot load class", e);
}
顺便说一下,在代码中添加日志记录将有助于找出意外行为背后的原因。
答案 2 :(得分:0)
Class.forName("SingleTask");
抛出了ClassCastException,因此mappers不会被填充。由于您忽略了构造函数中的ClassCastExeption
,因此您似乎错过了该错误。