我的 Facade 中的空指针:(

发布于 2024-10-08 09:36:08 字数 2369 浏览 4 评论 0原文

我写了一个任务管理器,好吧,这是一个很长的故事......顺便说一句,全部都是用 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);
    }


}

I've written a task manager, and well it;'s a long story... all in Java by the way. So I wrote a Facade which you can see below there is a problem with the HashMap and I suspect that the values which I attempt to add into the HashMap during the construction aren't going so well. The method that is triggering the null pointer exception is the create method. the input parameters to the method have been verified by me and my trusty debugger to be populated.

any help here would be great... I'm sure I forgot to mention something so I'll reply to comments asap as I need to get this thing done now.

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);
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-10-15 09:36:08

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

For Class.forName("RepeatingTask") to return a class you must have a class persistence.RepeatingTask. But in your comment you say that obj.getClass() returns domain.RepeatingTask so it looks to me like you have 2 "RepeatingTask" classes or domain.RepeatingTask is a sub type.

与之呼应 2024-10-15 09:36:08

我的猜测是您的问题在于构造函数:

    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) {}

您默默地忽略了 ClassNotFOoundException。如果您向捕获添加日志记录,我希望它会告诉您未找到类 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);
    }

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

My guess is that your problem lies in the constructor:

    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) {}

You silently ignore the ClassNotFOundException. If you add logging to the catch I expect it to tell you that the class SingleTask is not found, as I expect that you did not put those classes in the default package.

Given your reply to comments these classes are in the domain. package, so you could try to change to:

    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);
    }

Btw, adding logging to your code will help to find the reasons behind unexpected behaviour.

五里雾 2024-10-15 09:36:08

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

Class.forName("SingleTask"); is throwing a ClassCastException, so mappers does not get populated. Since you are ignoring ClassCastExeption in your constructor you have missed that error, it seems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文