| Forums: |
java
Android
|
| Author |
Why doesn't this get/set work properly?
|
Fiona Heiss
Greenhorn
Joined: Jan 02, 2013
Posts: 5
|
|
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
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
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.
|
Mohamed Sanaulla | My Blog
|
 |
Fiona Heiss
Greenhorn
Joined: Jan 02, 2013
Posts: 5
|
|
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
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
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
Joined: Jan 02, 2013
Posts: 5
|
|
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.
|
 |
 |
|
|
subject: Why doesn't this get/set work properly?
|
|
|