我的 Facade 中的空指针:(
我写了一个任务管理器,好吧,这是一个很长的故事......顺便说一句,全部都是用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使
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 classpersistence.RepeatingTask
. But in your comment you say thatobj.getClass()
returnsdomain.RepeatingTask
so it looks to me like you have 2 "RepeatingTask" classes ordomain.RepeatingTask
is a sub type.我的猜测是您的问题在于构造函数:
您默默地忽略了 ClassNotFOoundException。如果您向捕获添加日志记录,我希望它会告诉您未找到类
SingleTask
,因为我希望您没有将这些类放入默认包中。鉴于您对评论的回复,这些类位于
domain.
包中,因此您可以尝试更改为:顺便说一句,向代码添加日志记录将有助于找到意外行为背后的原因。
My guess is that your problem lies in the constructor:
You silently ignore the
ClassNotFOundException
. If you add logging to the catch I expect it to tell you that the classSingleTask
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:Btw, adding logging to your code will help to find the reasons behind unexpected behaviour.
Class.forName("SingleTask");
抛出 ClassCastException,因此映射器不会被填充。由于您在构造函数中忽略了 ClassCastExeption ,因此您似乎错过了该错误。Class.forName("SingleTask");
is throwing a ClassCastException, so mappers does not get populated. Since you are ignoringClassCastExeption
in your constructor you have missed that error, it seems.