我对这段代码感到困惑。它一直给我错误的“Deferencing Null Pointer”。
bookStore.java
@Override
protected store createOutlet (String storeName, String storeType) {
store theStore = null;
theStore.setStoreName(storeName); //getting Deferencing Null Pointer Error
theStore.setStoreType(storeType);
return theStore;
}
storeProducer.java
public abstract class storeProducer {
protected abstract store createOutlet(String storeName, String storeType);
public store createNewStore(String storeName, String storeType) {
store newStore = createOutlet(storeName, storeType);
newStore.createStore();
return newStore;
}
store.java
public abstract class store extends Observable {
abstract void createStore();
它有什么问题?我已经尝试过抛出异常,但仍然没有工作。
答案 0 :(得分:2)
theStore
为null
,theStore.anything()
将尝试取消引用空指针。
您似乎想要创建store
个实例,可以使用new
执行此操作:
store theStore = new AnyNonAbstractClassDerivingFromStore();