• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array .length not working as planned

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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];
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok for reference I was able to solve this null pointer exception with the following code.



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic