简体   繁体   中英

How to get superclass data member from subclass

I'm having trouble accessing a superclass data member from it's subclass.

So I have a superclass Store like this

public class Store {
    protected ArrayList<Audiobooks> ab;

    public Store(ArrayList<Audiobooks> ab)
    {
        this.ab = ab;
    }
    ...
}

ab is initialized and everything, then we have a subclass like this

public class Customer extends Store {

    ...
    public Customer(String id, String name, String address)
{
    this.id = id;
    this.name = name;
    this.address = address;
    }
    public void printAb(){
        for(int i = 0; i<ab.size(); i++){
            System.out.println(ab.get(i).toString());
        }
    }
}

I end up getting a null pointer exception error. When the function is placed in the store class it works fine, but when its in the subclass Customer the null pointer exception occurs.

I tried to get it by using super.ab.... but no success.

Thanks for any insight.

Your Customer constructor does not call you Store constructor, so the ab list is never instantiated.

To solve this, you either need to have your customer constructor call the store constructor:

public Customer(String id, String name, String address) {
    super(new ArrayList<Audiobooks>());
    this.id = id;
    this.name = name;
    this.address = address;
}

Or have your customer constructor instantiate the list. You also must have more than one constructor for the store class, otherwise the compiler would force you to call the constructor you are showing. You may want to consider getting rid of the empty constructor that you apparently have on the store class if it does not put the class into a valid state.

You access the ab property properly (otherwise you would have a compiler error).

Most likely you are not creating the object anywhere, try doing

 protected ArrayList<Audiobooks> ab = new ArrayList<Audiobooks>();

Also, it would be better if you use the most generic interface List

 protected List<Audiobooks> ab = new ArrayList<Audiobooks>();

Try doing this.

public class Customer extends Store {

    ...
    public Customer(ArrayList<Audiobooks> a,String id, String name, String address)
{
    super(a);
    this.id = id;
    this.name = name;
    this.address = address;
    }
    public void printAb(){
        for(int i = 0; i<ab.size(); i++){
            System.out.println(ab.get(i).toString());
        }
    }
}

or do this

public class Customer extends Store {

        ...
        public Customer(String id, String name, String address)
    {
        a=new ArrayList<Audiobooks>(/*initial size*/);
        this.id = id;
        this.name = name;
        this.address = address;
        }
        public void printAb(){
            for(int i = 0; i<ab.size(); i++){
                System.out.println(ab.get(i).toString());
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM