aspose file tools*
The moose likes Beginning Java and the fly likes Still working on this inventory project, issue with inventory array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Still working on this inventory project, issue with inventory array" Watch "Still working on this inventory project, issue with inventory array" New topic
Author

Still working on this inventory project, issue with inventory array

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Do you call startInv1, startInv2 and startInv3 somewhere ?


[My Blog]
All roads lead to JavaRanch
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
In the words of my grandfather "Wowy".

I should have known that... thanks.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jun 29, 2009
Posts: 84
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

Joined: Jun 29, 2009
Posts: 84
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jun 29, 2009
Posts: 84
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jun 29, 2009
Posts: 84
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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

Joined: Jun 29, 2009
Posts: 84
Yeah I realized that, I made another post about this issue here

http://www.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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Still working on this inventory project, issue with inventory array
 
Similar Threads
Weird multiple output issue with if statements
For Loop Produces Weird Output
Trying to add a count to my inventory through a constructor
Adding a new array element is not saving to memory
array .length not working as planned