• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing and modifying array from superclass  RSS feed

 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not 100% understood inheritance but from what I understand doesn't sub class that has been extended from Superclass inherit everything from superclass?

I am trying to access and modify array that's initialized in Superclass from Subclass.
However I have nullpointerexception at Line 27.
I am not able to access someList from superclass
What am I doing wrong?


Something like this.
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is thrown on line 29.
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've allocated the outer array: someList = new Sub[size][];
You've allocated the inner array: someList[i] = new Sub[size];
But you haven't initialized the someList[i][0] to refer to a  Sub object, hence, null pointer exception.

You're code is not well thought out. Sub is-a Super so you can't create a Sub instance in order to populate the arrays because then the constructors call each other: Sub-->Super-->Sub-->Super... infinite recursion.

Not sure what you are really trying to do here and I don't see an obvious work around.
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never reference a subclass directly in a superclass, period. In the object-oriented design world, this is as close as you can get to incest. Whatever horror you feel at the thought of that in the real world, that's the same kind of horror you should feel with that kind of design.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!