Tuesday, 15 June 2010

java - How to get superclass data member from subclass -



java - How to get superclass data member from subclass -

i'm having problem accessing superclass info fellow member it's subclass.

so have superclass store this

public class store { protected arraylist<audiobooks> ab; public store(arraylist<audiobooks> ab) { this.ab = ab; } ... }

ab initialized , everything, have subclass this

public class client extends store { ... public customer(string id, string name, string address) { this.id = id; this.name = name; this.address = address; } public void printab(){ for(int = 0; i<ab.size(); i++){ system.out.println(ab.get(i).tostring()); } } }

i end getting null pointer exception error. when function placed in store class works fine, when in subclass client null pointer exception occurs.

i tried using super.ab.... no success.

thanks insight.

your customer constructor not phone call store constructor, ab list never instantiated.

to solve this, either need have client constructor phone call 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 client constructor instantiate list. must have more 1 constructor store class, otherwise compiler forcefulness phone call constructor showing. may want consider getting rid of empty constructor apparently have on store class if not set class valid state.

java inheritance super

No comments:

Post a Comment