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 array .length not working as planned Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array .length not working as planned" Watch "array .length not working as planned" New topic
Author

array .length not working as planned

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok I have what I hope is the final question for this inventory project.

I am trying to access the .length or an array I have created to make this program more dynamic depending on how many elements are in the array.

This is my loop


This is the code that I am using to hard code my arrays for the time being


Would it be advisable that I somehow add a count to this, or is it possible to find out how many arrays are full with the above code?

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30913
    
158

Mike,
You have two variables named "newinv". It looks like the wrong one is in scope when you try to use newinv.length. The best thing to do is rename one. It would make the code less confusing and avoid this issue.

Newinv newinv = new Newinv();
public Inventory newinv[] = new Inventory[200];


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok it dawned on me to just place the for loop in the same class as the array. This works, however b/c the array is 200 elements large it has 197 null fields.

That produces this error

Exception in thread "main" java.lang.NullPointerException
at Newinv.getWhole(newinv.java:41)
at Option.userInput(option.java:61)
at Run.main(run.java:12)

Actually, I think I just had another dawn, as I am writing this. I am going to add a condition to check for null. that should work...

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Jeanne Boyarsky wrote:Mike,
You have two variables named "newinv". It looks like the wrong one is in scope when you try to use newinv.length. The best thing to do is rename one. It would make the code less confusing and avoid this issue.

Newinv newinv = new Newinv();
public Inventory newinv[] = new Inventory[200];


Thats a good idea, I am going to do that.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok for reference I was able to solve this null pointer exception with the following code.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the inventory can contain an arbitrary number of elements why use an array? What if you have more than 200 items? How do you find items in the inventory--loop through the entire array?

There are a disturbing number of static "utility" classes; that will create problems at some point. I'm hoping this isn't for dealing with real inventory.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
no its not, this is for a class, and the professor is making us use an array.

I looked into arrayList, but that looks like it might be beyond my greenhorn skills.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array .length not working as planned