引用空指针错误

时间:2015-12-03 03:26:14

标签: java nullpointerexception

我对这段代码感到困惑。它一直给我错误的“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();

它有什么问题?我已经尝试过抛出异常,但仍然没有工作。

1 个答案:

答案 0 :(得分:2)

theStorenulltheStore.anything()将尝试取消引用空指针。

您似乎想要创建store个实例,可以使用new执行此操作:

store theStore = new AnyNonAbstractClassDerivingFromStore();