gpt4 book ai didi

java - 我的 Facade :( 中的空指针

转载 作者:行者123 更新时间:2023-11-29 06:17:38 25 4
gpt4 key购买 nike

我写了一个任务管理器,好吧;说来话长……顺便说一句,全部用 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);
}


}

最佳答案

要使 Class.forName("RepeatingTask") 返回一个类,您必须有一个类 persistence.RepeatingTask。但是在您的评论中,您说 obj.getClass() 返回 domain.RepeatingTask 所以在我看来您有 2 个“RepeatingTask”类或 domain.RepeatingTask 是一个子类型。

关于java - 我的 Facade :( 中的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4477719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com