• Post Reply Bookmark Topic Watch Topic
  • New Topic

Addressbook advice  RSS feed

 
Brian Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been putting together an addressbook and try to work on some idea but I have hit a bit of a wal early on, and Im sure the problem is easy to resolve, I just can't get the old brain to do it.

At present I have 5 classes

  • PersonDetails
  • AdressDetails
  • AddressBook
  • AddressBookGUI
  • EnterDetailsPanel


  • I have taken EnterDetailsPanel out of the main GUI code, as I plan, or had planned on using additional JPanels for the other options of the application until I got stuck. What I want the EnterDetails panel to do, which it does so far is to have the user enter all of their information and when the button is pressed it will then create a PersonDetails object and here is where my problem is.

    I can't figure out how to get the object from the panel back across to the GUI which initalises the AddressBook class in the constructor and add it to the List via an addDetails() method.

    If I create a quick method in the AddressBookGUI class I can add the object to the List fine, I'm guessing this is because I have created a new instance of the AddressBook in here already via the constructor, however if I add an AddressBook reference in the EnterDetailsPanel I get a nullpointerexception for the opposite because it's not been created in there.

    These are what most of the classes do so far.

    PersonDetails has a reference to the AddressDetails and holds all information for that person.
    AddressBook contains a List that accepts PersonDetails and has methods to add/remove the person.
    AddressBookGUI creates a new instance of the AddressBook class along with the general gui things.
    EnterDetailsPanel is added to JTabbedPane in the GUI class and allows the user to enter their details and submit those via a button which will then create a new PersonDetails object that I am trying to get added to the AddressBook class List

    Any help would be most welcome!

    Brian


     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you show us what you have so far (and use code tags)? I'm assuming this is homework.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brian Blackburn wrote:At present I have 5 classes...

    Fraid the Wimbledon final is on soon, so you're likely to get responses (at least from me) in bits and pieces.

    The first thing I'd say is that your two "...Details" classes should just be Person and Address. Also: EnterDetailsPanel is not specific, unless it's a panel to enter details for any of your classes.

    Winston
     
    Brian Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the replies.

    Here are the two culprits, I will make amendments to the naming of the panel class too thanks Winston.

    When the button is clicked in the panel the information in the fields is used to create a new Person object, this is where I am presently stuck about how to get this object back over to the AddressBookGUI class and added to the AddressBook's list where I already have an existing addDetails method.

    As mentioned above if I do this within the AddressBookGUI class just doing a test run of it the object is added fine, I'm just having issues on getting the created object in the panel back across. I've tried creating a reference to the class in the panel but when I call the addDetails method I'm getting a nullpointerexception, which will be due to me not creating an instance of the class when I was trying to figure it out.

    AddressBookGUI

    EnterPersonDetailsPanel
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does the address book work without a GUI?
     
    Brian Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Everything worked fine when I was testing the code out using the console to confirm entries and so on, but that was done in a single class, which created a new AddressBook instance from the main methd and had some other methods in there to get user input.

    The idea of wanting to keep the panels seperate was purely to try and keep things tidy, if I move the panel back into the same class as the main GUI code it works fine.
     
    Brian Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A small update. I have made some progress on the problem by passing in a reference to the AddressBook class to the panels constructor which seems to work ok.

    For the purpose of the initial problem is something like this a valid method to use for getting these values across to where I need them to be, as I don't want to start doing something which is considered a bad habit just for the sake of getting something to work.

    Brian
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not an expert, but I think IRL you wouldn't be passing a value back anywhere; you would deal with it right where it was. The couple of times I've worked directly with GUIs (and not something like a web page) I've done all my work in the callbacks of the buttonPress events, or whatever is activating your GUI.
     
    Brian Blackburn
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agreed there is no doubt a better way of doing anything, but at present I can't think of one and it's a continuous learning game anyhow.

    Whether or not keeping all the components in one main class along with the rest of the general GUI code is another way of doing it I don't know, and it would just become full of additional bits and pieces as and when they were added if that was the case.

    I have heard briefly about the MVC Pattern, so will look into this also so gain more insight n the way I should ideally be laying out the components and other code.

    Thank you for all your help guys, great as always.

    Brian
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!