Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this get/set work properly?

 
Fiona Heiss
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use a get/set to set variables in one class, and access it in another, but I can't get it to work. It doesn't return null, but it returns a size of 0 on the line: ArrayList<Integer> sizes = handler.getSizes();

Here is where I call the mutator:



And here is my slightly edited TMXHandler.java file (irrelevant bits removed):



I am using Android TMX Loader.

Using the debugger I have verified that getSizes is executed before setSizes, so I have no idea why it won't work for me.

Any help would be much appreciated.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line:
private ArrayList<Integer> sizes = new ArrayList<Integer>();
is instantiating a new instance of ArrayList and hence the sizes reference variable is not null and points to a valid ArrayList instance of size 0.

Assuming the line above is part of the class declaration and not present withing any method.
 
Fiona Heiss
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I removed that part, but now I'm getting NullPointerExceptions when I call setSizes, and when I try to add to the ArrayList sizes.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nullpointer exception is because the ArrayList is null and doesn't reference any object. Your earlier code was correct as it would initialise the ArrayList before inserting the data into the list.
I think I havent understood your query correctly. Sorry for that.

Fiona Heiss wrote:I am trying to use a get/set to set variables in one class, and access it in another, but I can't get it to work.

Can you tell us what exactly isn't working? (Before you made the change I mentioned in my first reply).
 
Fiona Heiss
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I am trying to call

in the method loadWorld(). I am doing this to use the variables from the TMXHandler.java class for the following line:

But, I get a NullPointerException on that line.

Does that make more sense? I'm not sure how to explain it any better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic