• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array problem  RSS feed

 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heya!
Making a program where you write your name and age, then you should be able to look at it and others before you (that have filled out the form).
My problem is that the arrays in "Person" Class wont store the values i give it from the "Form Class"...(just throws me the Null)..
Have been Googeling it and I think it has something to do with the initializing, but i don't seem to find the answer...
Any kind soul out there?

My Code: https://github.com/phunlife/FirstDatabase



 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andreas,

If you post the relevant parts of your code here (remembering to use code tags) it's more likely that people will help you.
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Person is local to a Form object yet your Form object goes out of scope in MainMenu taking any filled in data along with it.
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Hi Andreas,

If you post the relevant parts of your code here (remembering to use code tags) it's more likely that people will help you.


Ah, ok. Thanks!

Carey Brown wrote:

Person is local to a Form object yet your Form object goes out of scope in MainMenu taking any filled in data along with it.


Hmm, think i understand what you mean, will check it when i get home. Thanks!

 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This part of the code is what i need help with.



Hope i did it right :P
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is posted correctly.

Your nextID variable is being overwritten at the beginning of the store method. BTW why are you using nextID as the index to get the nextID from the myID array (which appears to be empty unless it is filled in code you haven't supplied).

The above is somewhat irrelevant though as the design of the Person class is totally wrong. A Person class should hold the details of a single person, not 10 people. If you want to store the details of 10 people create 10 instances of the Person class and put them into an array, Collection or possibly some other class that naturally holds a number of people ie Team, Squad, Platoon etc.
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Yes that is posted correctly.

Your nextID variable is being overwritten at the beginning of the store method. BTW why are you using nextID as the index to get the nextID from the myID array (which appears to be empty unless it is filled in code you haven't supplied).

The above is somewhat irrelevant though as the design of the Person class is totally wrong. A Person class should hold the details of a single person, not 10 people. If you want to store the details of 10 people create 10 instances of the Person class and put them into an array, Collection or possibly some other class that naturally holds a number of people ie Team, Squad, Platoon etc.


Yes, forgot to change that before i posted the code... :P
Oh you got a point there sir! Will try to remember that.
Thank you!

Carey Brown wrote:
Person is local to a Form object yet your Form object goes out of scope in MainMenu taking any filled in data along with it.

You my fine sir should have a medal!
Thanks for the help!

Solved it!
Full code at: https://github.com/phunlife/FirstDatabase

Solution:
- Removed the "Person" class from "MainMenu". (the array got saved there and not in the "Form")
- Added in MainMenu:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Tchikatchika wrote: . . .
- Removed the "Person" class from "MainMenu". (the array got saved there and not in the "Form")
- Added in MainMenu:
Both of which hide other serious problems. The Person class should be completely separate from the GUI display. The person should not be a non‑private field of the form object. In fact it probably should not be in the form object at all. There should be some object somewhere with Person fields, but it should be independent of the GUI.
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Andreas Tchikatchika wrote: . . .
- Removed the "Person" class from "MainMenu". (the array got saved there and not in the "Form")
- Added in MainMenu:
Both of which hide other serious problems. The Person class should be completely separate from the GUI display. The person should not be a non‑private field of the form object. In fact it probably should not be in the form object at all. There should be some object somewhere with Person fields, but it should be independent of the GUI.


Serious problems? (If you got the time please give a link or describe it)
Do not really understand how too make that, but i will search for it! Thanks for the heads up!

 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Person should only contain the (private) fields necessary to describe a single person, ie no arrays, just name and age, along with getters and a constructor and probably a toString() method. Your MainMenu should have an ArrayList<Person> where the id is the index into the array. Form should return a Person object that the MainMenu adds to the list. This is what I'd probably implement off the top of my head.

P.S. Are you keeping your code in github up to date?
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Person should only contain the (private) fields necessary to describe a single person, ie no arrays, just name and age, along with getters and a constructor and probably a toString() method. Your MainMenu should have an ArrayList<Person> where the id is the index into the array. Form should return a Person object that the MainMenu adds to the list. This is what I'd probably implement off the top of my head.

P.S. Are you keeping your code in github up to date?


Oh you just saved me a lot of thinking! :p I will look into it today or tomorrow, thank you!
Yes, i update it as often i can
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!