[英]Getting a weird null pointer exception
I get a null pointer exception at this line : 我在这行得到一个空指针异常:
private ArrayList<DrawableEntity> entitiesToDraw = Loader.instance().getDrawableEntities();
There is apparently no problem with the constructor of Loader : 显然,Loader的构造函数没有问题:
public static Loader instance() {
if (instance == null) {
new Loader();
System.out.println("Loader ready");
}
return instance;
}
Because I get the message "Loader ready". 因为我收到消息“ Loader ready”。 I don't understand, the problem seem to be before the call to getDrawableEntities() but I see nothing and it is not inside getDrawableEntities().
我不明白,问题似乎出在调用getDrawableEntities()之前,但我什么也没看到,而且它不在getDrawableEntities()内部。
You forgot to assign it to instance
您忘记将其分配给
instance
public static Loader instance() {
if (instance == null) {
instance = new Loader();
System.out.println("Loader ready");
}
return instance;
}
And by the way, if thats a singleton then it's wrong (not thread-safe), Here's a way to implement the singleton pattern . 顺便说一句,如果那是一个单例,那是错误的(不是线程安全的), 这是一种实现单例模式的方法 。
public static Loader instance() {
if (instance == null) {
instance = new Loader();
System.out.println("Loader ready");
}
return instance;
}
You forgot to asign your instance
variable 您忘记分配
instance
变量
You are not setting instance value when its null. 您未设置实例值为null时。 It should be:
它应该是:
if (instance == null) {
instance = new Loader();
System.out.println("Loader ready");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.