所以我有一个具体的类和一个抽象类,我试图从抽象类中访问具体类中的方法。 Store目前包含许多成员类所需的getter。目前获取空指针异常。
public abstract class members{
// Trying to refrence the store object
Store store;
public void someMethod(){
// I want to be able to access all the methods from the store class
// eg
store.showVideoCollection();
}
}
public class store {
// This class has already been instantiated, just one object for it.
public void showVideoCollection(){
// Stuff here
}
public void otherMethod(){
// Stuff here
}
}
编辑:
主要方法
public class start {
public start() {
store = new Store(); // Don't want to create more than 1 store object.
}
由于
答案 0 :(得分:1)
为了存储Store
实例,您必须实例化它。因此,您声明变量store
但您从未初始化它(因此它null
)。我想你想要像
// Trying to refrence the store object
Store store = new Store(); // <-- create a Store and assign it to store.
或者,您可以将Store
设为Singleton。链接的维基百科页面(部分) 单例模式是一种设计模式,它将类的实例化限制为一个对象。
public final class Store {
public static Store getInstance() {
return _instance;
}
private static final Store _instance = new Store();
private Store() {
}
public void showVideoCollection(){
// Stuff here
}
public void otherMethod(){
// Stuff here
}
}