1

我已经写了一个任务管理器,而且它很长。顺便说一句,所有这些都是用 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);
    }


}
4

3 回答 3

2

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

于 2010-12-18T11:34:17.267 回答
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。如果您将日志记录添加到捕获中,我希望它会告诉您找不到该类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);
    }

顺便说一句,将日志记录添加到您的代码将有助于找出意外行为背后的原因。

于 2010-12-18T11:36:17.847 回答
0

Class.forName("SingleTask");正在抛出 ClassCastException,因此不会填充映射器。由于您ClassCastExeption在构造函数中忽略了该错误,因此似乎。

于 2010-12-18T11:37:32.487 回答