aspose file tools*
The moose likes Beginning Java and the fly likes Adding a new array element is not saving to memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding a new array element is not saving to memory" Watch "Adding a new array element is not saving to memory" New topic
Author

Adding a new array element is not saving to memory

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am attempting to use an inventory class to create an array inventory element like so



However when this is all said and done, and the user is returned to the main screen and I reprint my inventory with the following code



The above code only outputs my old inventory, is there any reason for this? it seems like the array element is lost from memory.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
My suspicions were realized when I added <b>inventory.newinv[3].getInvName()</b> to the print of the inventory and I got a null pointer exception.

Why would the newly created inventory not save??

*edit
Also if I add the for loop to the part of the program that adds a new inventory it prints correctly. So I am thinking that after the somehow reprinting the menu is causing that spot in memory to be erased.

In order to print the menu, the user has to make a new selection, during this process the new inventory item is being losed...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Why have you got objects with non-private fields, like inventory. ?

How many "inventory" references do you have? You may have two variables which refer to different object. One may be a local variable.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am not too worried about public or private at this point.

Also I am sure that I am creating the new inventory item correctly because it outputs fine with the for loop right after creating the inventory.

It isent until the user is redirected back to the main menu, that the new inventory seems to disappear and I am only left with the initial inventory that I have hard coded.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Where are you creating this inventory? Is it a local variable?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
THis is where I have hardcoded my original inventory



And this is the inventory class


I have narrowed it down to being an issue with the following code


This allows the user to enter the new inventory item by selecting 2 from the MAIN MENU, and then return to the main menu to select 1 to print the inventory. At some point when the user returns to the main menu the new array element is lost. I know the array is correct prior to this because I added the for loop that is used to print the inventory(currently in selection1) to the if statement for selection 2 and it prints the full inventory.

Thanks for looking into this one, I am totally lost. I was told the garbage collector was a great feature, but it looks like the garbage was brought to the road too soon in this program.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Osterhout wrote:THis is where I have hardcoded my original inventory
Thanks for looking into this one, I am totally lost. I was told the garbage collector was a great feature, but it looks like the garbage was brought to the road too soon in this program.

The odds that garbage collection is in any way responsible or even involved in this error are exceedingly small. It is likely that there's an error in your code, perhaps in code you've not posted yet.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am not sure if this will help, but this is my complete suboption class. It simply gets the users selection and acts on it similar to the way the main menu works. This submenu is only printed if the correct option is selected from the main menu.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Osterhout wrote:I am not sure if this will help, but this is my complete suboption class.

Hard to say as there are several other classes that this depends on that we don't have available.

Here are some problems that I'm seeing off the bat, that perhaps have nothing to do with your original question/problem:

1) Submenu seems to be used as both an object and a class with static methods:

I'm sure that you remember our previous discussion about use and abuse of statics. Again, avoid them unless you know for certain that you should be using them here. Having said that, the subInput method itself is static, and I have to wonder why. Is it being called straight off of the main method?

2) there's a Newinv method being called, startInv3, that is not found in the Newinv code.

3) Here,

you're wandering off in to recursion. Are you sure that you want the subInput method to call itself recursively? This can lead to big-time badness. In fact, you appear to be doing this a lot, and I have to wonder if this is your main problem. When you call a method recursively, it creates all new local variables and so many Newinv objects may be created, one with a 3rd item, others (probably the one you care about), not. I would strongly advise you to get rid of recursion.

I get the feeling that there is a lot of redundancy in this code including the many classes with the term "menu" in them, that it can be simplified greatly and that this would make debugging much easier.

My main recommendation here is to scrap this class and start afresh, but to plan out what you are going to do before trying to commit code to IDE.

Best of luck
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Yeah, way back somebody attempted to help me with the recursion issue. I was just not understanding how with those if statements I could include the class only once. Also you are right about getting rid of some of the classes. Even though I have 3 menus, they should all be in one class, and even tho I have 3 option/if then statements there should only be 1 option class.

I am just confused as to how I could write those if statements and not include the, print menu/ option/ selection code over and over again.

If I can understand that I think it would be wise to rewrite this...
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Recursion is your main issue here, so I'm going to try to illustrate the recursion problem with a simplified version of your program. Say in place of Inventory, I have a simple FooInventory class like so:


Now say I create a SubOptionSimple class that somewhat mimicks yours:


Now let's walk through my SubOptionSimple and see what happens.

1) The main method first calls the static subInput method, and it creates a FooInventory array called fooInvs that has places for 100 FooInventory items. When the array is created, all items in the array are initially set to null.

2) I then initialize the first two items in this array, items 0 and 1 with the names Pete and Mike (they sounded good for some reason).

3) I next display my menu String and which requests input.

4) Assuming that the user types "2", then enter, then my first if block is run. It asks the user to enter a name, (say he enters "John") accepts the String, then creates the third FooInventory item (fooInvs[2]) using this String, updating my array.

5) It then does something bad, it calls subInput recursively. This restarts the subInput method from the beginning which creates a new FooInventory array that is completely different from the one we've been working with. This point is very important (which is why I've italicized it). It displays the menu String requesting user input.

6) Say the user chooses "3" causing the else if (....'3') block to run, this menu will then use a for loop to loop through and display the contents of the new fooInvs FooInventory array, an array which has not received the input in the if block above it.

Do you see how this will cause your program to fail?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
So how to fix this? Rather than use recursion, use a loop such as a while loop. For instance:

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ya, I understand that. In my mind I would think that the inventory class would save separately from the menu/selection but it appears that my recursion is causing the inventory to be reset.

So my solution is to start from the beginning. These are the 3 classes that will be the foundation to all of my menus in this program.
From there I will just need the inventory classes.

Right now I am having a problem with some more recursion and I think it starts from my Run class. The program prints the complete menu twice each time before printing which selection was chosen.


This is the new menu class, I am going to attempt to use one function to both print the menu and grab the selection.



This is my new extremely simplified option class that I will attempt to remove the recursion from


Lastly this is the run class that I think is giving me my issue


When I actually run the program, it reprints the menu twice everytime... This is the same issue that I was having with my option class.

If I can get this figured out, hopeful I can do the same with my option class and I will report back...
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Osterhout wrote:Ya, I understand that. In my mind I would think that the inventory class would save separately from the menu/selection but it appears that my recursion is causing the inventory to be reset.

that would perhaps be the case if the array were defined outside of the method, but regardless if this would cause a temporary fix, the right answer is not to use recursion here. A while loop works so much better and is much safer.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
so is a while loop what I need in the run class, if so can you show me how that would look?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Osterhout wrote:
Lastly this is the run class that I think is giving me my issue

When I actually run the program, it reprints the menu twice everytime... This is the same issue that I was having with my option class.
If I can get this figured out, hopeful I can do the same with my option class and I will report back...

You call menu.Main() from within the option class, and it gets and returns input for you. Why are you calling it in the main method above only to discard the input? Get rid all references to Menu in this class above. Also, don't forget to put a while loop in your Option code.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Osterhout wrote:so is a while loop what I need in the run class, if so can you show me how that would look?

Please see code already posted above.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I was thinking to myself before I commented out the menu in the Run class, there is no way that this going to work b/c the program wont know to print the menu.

However when I created the selection integer, I also create the new menu and as it turns out that has been a cause to my problems. Also that was one reason I was having a hard time with the static vs non static stuff.

In the end to anyone who reads this, be careful when using public methods that output to the screen... *<-edit it is mostly tricky when that output also returns a value.

I think I said that right, now for implementing this while loop...

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
ok Pete I hate to do it but I have to ask for some advice on this while loop.

My idea is to add the following code


I have tried it at the start, end, and even wrapped it around the if statements to no avail. Am I completely off base?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I reread the thread and I saw you gave an example however when I added it it prints " your entered a 1" everytime

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok I can put an end to this thread...

Here is our new menu class


And here is our new option class


Nobody said learning this stuff would be easy. In order to succeed at a challenge like this we really have to be persistent and never give up.

Also it doesn't hurt to have a forum like this to expedite the process.

Thanks a lot Pete and everyone else who has been helping me not only learn java, but learn it the right way.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You're quite welcome, and best of luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding a new array element is not saving to memory