File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why doesn't this get/set work properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why doesn Watch "Why doesn New topic
Forums: Beginning 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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't this get/set work properly?