Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes ArrayList with polymorphism and inheriting it to another class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList with polymorphism and inheriting it to another class.. " Watch "ArrayList with polymorphism and inheriting it to another class.. " New topic
Author

ArrayList with polymorphism and inheriting it to another class..

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
I have one class that holds an ArrayList of an abstract class. Two different classes extend this abstract class. I fill the array with either one or the other of the two. The problem is, in an outside class when I set a reference to the object that contains the class holding the ArrayList, I cant access any instance variables from it.

If I check the ArrayList's size the size is holding the correct value, but displays null when I print out a value..

Ex:

System.out.println(questionPanel.questions.get(0).answer);
gives me null.. but it holds a value..
but

System.out.println(questionPanel.questions.size());
shows 3..

How do I access the values out of the ArrayList from another class properly?

Thanks!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

Joseph Cho wrote:System.out.println(questionPanel.questions.get(0).answer);
gives me null.. but it holds a value..

Well that sounds right; otherwise the .answer bit would have thrown NullPointerException.

How do I access the values out of the ArrayList from another class properly?

Exactly as you are doing; it doesn't sound to me like that's your problem. The fact is that answer is null, not the ArrayList entry.

However, one tip I would give you is that stringing lots of derefs ('.') together can make things difficult to follow, so why not just break them up? viz:It may seem like a lot more code, but it'll help an awful lot in debugging.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Thanks for the response Winston; I tried it the way you suggested and I still get a null print.. Guess I will just have to keep playing around with it.

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Gosh dang it..

I found the issue..

Question(String question, String Answer)
{
this.question = question;
this.answer = answer;
}

inside the constructor, I capitalize a in answer .. doh!

Thanks for the help heh though and at least I gained some knowledge from you still.

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList with polymorphism and inheriting it to another class..
 
Similar Threads
commit() and rollback() problem!
instance variables initialization
removing a row from an array
my last question for this program.
Declare an Array?