• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Still working on this inventory project, issue with inventory array

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I got just about done with this project when I realized the array I had created would not allow me to insert, delete or update the inventory.


This is the inventory class that should allow for creating new inventory objects.



This is the newinv class which I am attempting to hard code the initial inventory into, that is not going so well...


I am trying to access the inventory with a code like this


The problem is I am getting a nullpointerexception. I think that if I can start to work with this data, I can figure out how to insert and delete, but this is really got me stumped.

Thanks for any pointers.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you call startInv1, startInv2 and startInv3 somewhere ?
 
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
In the words of my grandfather "Wowy".

I should have known that... thanks.
 
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 here is a stumper, when I use the following code it just grabs the last array and adds that up three times. So it grabs the wholesale value of .83 and adds that three times. Why wouldent it grab the value from newinv[1], then newinv[2], etc.

The true goal is to use the .length method but that is returning a null pointer exception again, so if I can get the hard code to work that would be victory.

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These three methods smell wrong to me:

You're creating one Inventory object but you are returning an array of them, and that's not right. It's not the likely source of your current problem, but will set you up for future problems. Also, newinv array should not be static.

If I had a method that created an Inventory array, I'd return the array. If it just created a single Inventory object it would return just that object. Myself, I'd give the Inventory class a constructor that takes a name String, a wholesale double, a retail double(though I cringe at using floating point to represent currency), and a count int and would get rid of these hard-coded methods altogether.
 
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
Is there a way that I could return just that one inventory object that was created rather than the array of them?

Also when I make newinv[] non static it gives me the nonstatic variable cannot be referenced from a static context.
 
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
I changed my startinv methods to look like this



This does not help the problem of the bad for loop, any ideas?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:Is there a way that I could return just that one inventory object that was created rather than the array of them?


yes, not trying to sound trite, but you just return one inventory object, simple as that. You of course change the signature of the method so that it returns Inventory not Inventory[]. The only thing that should be static here in this simple class / program is the main method; that's it.

Another thought, you could make the method return nothing, void, but have it update the array:


then have another public method to return the Inventory array if another object needs it, something like



Also when I make newinv[] non static it gives me the nonstatic variable cannot be referenced from a static context.


Then you're trying to access it in a static way somewhere, and that's where the problem is. So solve the problem there, but don't try to solve this by making it static though.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:I changed my startinv methods to look like this



This does not help the problem of the bad for loop, any ideas?



Again, statics really do not belong here
 
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 when I take your advice and make newinv[] and startinv1() non static I am unable to reference any of there stuff.



I think that this is a key concept to understand. Can you provide some insight?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:Ok when I take your advice and make newinv[] and startinv1() non static I am unable to reference any of there stuff.


because you're still trying to access it in static way. Create an Newinv object, and use this to call your methods of the class.

I think that this is a key concept to understand. Can you provide some insight?


Not as good as the tutorials can explain this. Please read up on creating instances or objects in the tutorials. It will explain this better than I can.
 
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
Well that was a good mini lesson on calling your methods statically... However unfortunately I still get that crazy output.


This is the code for my for loop. It simply grabs the startinv3 array 3 times, NO IDEA WHY.


This is the inventory code which is used to create new inventory objects.



And this is the modified newinv class which is no longer static and is currently being hardcoded to create inventory elements insinde the inventory array, eventually I will hopefully use this to allow the user to add inventory.



For now I am simply lost as to why that for loop produces command line output like this:

0.83
0.83
0.83
the total wholesale is $2.4899999999999998
press the enter key to continue

notice it grabs the last array 3 times...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are your the fields of your Inventory class all static ? If you do that, all instances will share the same value, which explain why you have the same output for each instance you have created.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Osterhout wrote:
notice it grabs the last array 3 times...



It doesn't. Every time you set the value of any one of those Inventory objects the value is share among all objects of that class.

Do you get a warning when you do something like:
this.retail = retail;

there is no "this.retail" all Inventory objects have the same retail value, it isn't specific to each instance... or 'this' instance.

Create an attribute that is not static on your inventory object and you can see it happening, I think it'll be a good teaching tool for you. Keep the rest of your code the way it is. You'll see the non-static variable be remembered for each instance of the object.
 
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
Yeah I realized that, I made another post about this issue here

https://coderanch.com/t/453727/Java-General-beginner/java/Loop-Produces-Weird-Output

Making the variables static made it so when a new object was created its variables overwrote the previous objects. Thus making all objects have the same info.

Now the trick is allowing the user to create and delete inventory.
 
reply
    Bookmark Topic Watch Topic
  • New Topic