-1

I am kinda confuse with this piece of code. It keeps give me the error of "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();

What the problem in it? I already tried like throwing the exception but still not working.

Chuah Cheng Jun
  • 243
  • 1
  • 3
  • 17

1 Answers1

2

theStore is null and theStore.anything() will try to dereference the null pointer.

It seems you wanted to create a store instance, you can do that with new:

store theStore = new AnyNonAbstractClassDerivingFromStore();
Emil Laine
  • 41,598
  • 9
  • 101
  • 157