我已经写了一个任务管理器,而且它很长。顺便说一句,所有这些都是用 Java 编写的。所以我写了一个外观,你可以在下面看到 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);
}
}