Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 7 of 9 FirstFirst ... 56789 LastLast
Results 151 to 175 of 209

Thread: Address Book Program Issues

  1. #151
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Thanks your being a great help

    Still having this issue with my addEntry() method

    It compiles fine, but when i finish entering the last piece of data i get an error
    and the line of code is -

    "java.lang.String.NullPointerException: null"

    list[top] = entry;

  2. #152
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Looking back at what you said about this:

    The TextUI class needs a reference to the AddressBook object that the addEntry method can use to call a method in the AddressBook class to add the entry the addEntry method has created.

    I need to create a method inside of AddressBook that is called by the addEntry method.
    And the method inside the AddressBook class - function is to create an entry

    Is that right?

  3. #153
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    What variable is null? Make sure you give the variable a valid value before you use it.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  5. #154
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    I thought you had your design done and were now ready to code the program.
    Look back through the posts to see what you decided to do where and when. Then do that.

    Before you do any more coding you need to get a design for the program. What parts of the program do what and store what.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  7. #155
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    My AddressBook class needs to hold a collection of contacts.
    So in my addressbook class i need to be able to create contact object and in an array.
    These contact object need to be the entry that i'm using here
    so the contact object is created stored in addressbook.

    AddressBook entry = new AddressBook();
        list[top] = entry;
        top++;

  8. #156
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    This is in addressbook class

    public AddressBook()
        {
            list = new Contact[MAXENTRIES];
        }

    that i believe makes contact an arraylist with maxentries being the most amount of contacts allowed in the addressbook and list being the name of the arraylist

  9. #157
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Address Book Program Issues

    Norm, you have the will of a wizard! 16 Pages, and still not even a 'thanks'. My oh my .
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #158
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    I'm living in Missouri, a state famous for its mules.

  11. The Following User Says Thank You to Norm For This Useful Post:

    newbie (August 25th, 2011)

  12. #159
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Address Book Program Issues

    Quote Originally Posted by Norm View Post
    I'm living in Missouri, a state famous for its mules.
    I giggled
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  13. #160
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    I was more focussed on getting this sorted for tomorrow tbh and then spend the time to go through and thank every single post.

  14. #161
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Address Book Program Issues

    Would never expect you to thank every single post. I was just noting the amount of time norm has devoted to this.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  15. #162
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Sorry i'm not use to forums i didn't realise the button meant so much, but i have said during how much i appreciated the time and the help and i'm sorry i'm a pain in the ass with all this stuff i don't like asking so many things and don't want to waste peoples time but i really need the help to finish this before tomorrow or i'm screwed

  16. #163
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    So i've finally sorted out my AddressBook class i think and still sorting out the addEntry method in the Textui

    Having two issues, 1st the try{ is an unreachable statement and i can't figure out why
    2nd is the
    Contact contact = new Contact(name, address);
    i need to make a contact out of all that information but still not sure how
    public void addEntry()
            {
     
                System.out.println("Enter Contact Details: ");
                String forename = "";
                String surname = "";
                String street = "";
                String city = "";
                String county = "";
                String country = "";
                String post = "";
     
     
               if(addressbook.isFull())
                    System.out.println("Address Book is full");
                    return;
     
                //asks the user for the data of the address book
                try{
                    System.out.print("Forename: ");
                    forename = keyIn.readLine();
                    System.out.print("Surname: ");
                    surname = keyIn.readLine();
                    System.out.print("Street: ");
                    street = keyIn.readLine();
                    System.out.print("City: ");
                    city = keyIn.readLine();
                    System.out.print("County: ");
                    county = keyIn.readLine();
                    System.out.print("Country: ");
                    country = keyIn.readLine();
                    System.out.print("Postcode: ");
                    post = keyIn.readLine();
     
                }catch(Exception e){
                    System.out.println(e);
                    System.exit(0);
                }
                Name name = new Name(forename, surname);
                Postcode postcode = new Postcode(post);
                Address address = new Address(street, city, county, country);
     
                Contact contact = new Contact(name, address);
                addressbook.addContact(contact);
            }

  17. #164
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Address Book Program Issues

    By using 'return;' before try, It will exit at that point, meaning it will never go further down to the try catch.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  18. The Following User Says Thank You to newbie For This Useful Post:

    Gamb1t (August 25th, 2011)

  19. #165
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    The problem is the code is indented but it's missing the {}s.
    ALWAYS code the starting { and the ending } down one line when coding an if statement. Start with this and THEN fill in the code inside of the {}s
       if(some condition) {
     
       }

  20. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  21. #166
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    thanks,

    now onto show all entries:

    In my Contact class

    public void showContact(Contact contact)
            {
                 for( int index = 0; index < top; index++)
                    {
                        System.out.println((index+1)+" Forename:"+
                        list[index].toString());
                        System.out.println((index+1)+" Surname:"+
                        list[index].toString());
                        System.out.println((index+1)+" Street:"+
                        list[index].toString());
                        System.out.println((index+1)+" City:"+
                        list[index].toString());
                        System.out.println((index+1)+" County:"+
                        list[index].toString());
                        System.out.println((index+1)+" Country:"+
                        list[index].toString());
                        System.out.println((index+1)+" Postcode:"+
                        list[index].toString());
                    }
            }

    In my AddressTextUi Class:

    public void viewEntries(){
                  //checks if the address book is empty
                  if(addressbook.isFull()){
                    System.out.println("Address Book is full");
                    return;
                }
                addressbook.showContact(contact);
     
           }

    It can't find the constructor for Contact and i put

    Name name = new Name(forename, surname);
                Postcode postcode = new Postcode(post);
                Address address = new Address(street, city, county, country);
                Contact contact = new Contact(name, address);
    It can't find the variables so do i have to list them all again?

  22. #167
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    It can't find the constructor
    When you get errors you must post the full text of the error messages.

    This is interesting. If the list is full you don't allow it to be viewed:
     if(addressbook.isFull()){
                    System.out.println("Address Book is full
                   return;
               }
    Last edited by Norm; August 25th, 2011 at 05:22 PM.

  23. #168
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    oops good point.

    Added to Address class:

    boolean isEmpty() {
                return top = 0;
               }

    However it expects int so if i use
    int isEmpty() {
                return top = 0;
               }
    Then it compiles in Address Class but in my textui

       public void viewEntries(){
                 if(addressbook.isEmpty()){
                    System.out.println("Address Book is full");
                    return;
                }
           }

    It has error message - "incompatible types - found int but expected boolean"

  24. #169
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    Use = to assign a value to a variable. An assignment returns the value (and type) of the left hand operand.
    Use == to compare two operands. It returns a boolean value.

  25. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  26. #170
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Damn of course i missed it.

    So now
    public void viewEntries(){
                 if(addressbook.isEmpty()){
                    System.out.println("Address Book is full");
                    return;
                }
     
                Contact contact = new Contact(name, address);
                addressbook.viewContact(contact);
     
           }

    However it can't find variable name and address.

  27. #171
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    However it can't find variable name and address.
    where is it looking?

  28. #172
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    I think it's looking at the Contact class:

     public class Contact
    {
        private Address address;   
        private Name name;
     
     
        public Contact(Name name, Address address)  
        {  
            this.name = name;
            this.address = address;
        }     
     
        @Override  
        public boolean equals(Object obj) {  
            if (this == obj)  
                return true;  
            if (obj == null)  
                return false;  
            if (getClass() != obj.getClass())  
                return false;  
            Contact other = (Contact) obj;  
            if (address == null) {  
                if (other.address != null)  
                    return false;  
            } else if (!address.equals(other.address))  
                return false;  
     
            if (name == null) {  
                if (other.name != null)  
                    return false;  
            } else if (!name.equals(other.name))  
                return false;  
            return true;  
        }  
     
        @Override  
        public String toString() {  
            return super.toString() + "\n" + address + "\n" + name + "\n";  
        }  
    }

    i don't think my AddressBook class declares a new contact

  29. #173
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    Should it?

  30. #174
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    This just declares a Contact arraylist doesn't it , if its pointing at Contact class it shouldnt matter should it as if its declared in AddressBook it would be the same i think as it would be the name and address

    public class AddressBook
    {
        //index of the last entry
        private int top = 0;
       //constant number that indicates the maximum
       //number of entries in the address book
       private static final int MAXENTRIES = 5;
      //array of Address Book Entries
       private Contact[] list;
     
        /**
         * Constructor for objects of class AddressBook
         */
            public AddressBook()
            {
            list = new Contact[MAXENTRIES];
        }
     
     
         public Contact getContact(int n)
         {
             if(n >= top)
                return null;
                return list[n];
         }
     
         public void addContact(Contact contact)
            {
              if(top == MAXENTRIES)
               throw new IllegalStateException("The Addressbook is full");
              list[top++] = contact;
            }
     
          public void removeContact(Contact contact)
            {
              if(top == MAXENTRIES)
               throw new IllegalStateException("The Addressbook is full");
              list[top--] = contact;
            }
     
            public void viewContact(Contact contact)
            {
                 for( int index = 0; index < top; index++)
                    {
                        System.out.println((index+1)+" Forename:"+
                        list[index].toString());
                        System.out.println((index+1)+" Surname:"+
                        list[index].toString());
                        System.out.println((index+1)+" Street:"+
                        list[index].toString());
                        System.out.println((index+1)+" City:"+
                        list[index].toString());
                        System.out.println((index+1)+" County:"+
                        list[index].toString());
                        System.out.println((index+1)+" Country:"+
                        list[index].toString());
                        System.out.println((index+1)+" Postcode:"+
                        list[index].toString());
     
                    }
            }
     
            boolean isFull() 
             {
                 return top == MAXENTRIES;
             }
     
            boolean isEmpty() 
            {
               return top == 0;
            }
    }

  31. #175
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Address Book Program Issues

    This just declares a Contact arraylist doesn't it ,
    You have posted what looks like the definition for the AddressBook class.
    What is the "this" you refer to?
    Also where is the arraylist? I see an array but no arraylist.

    it would be the same i think as it would be the name and address
    I have no idea what you are talking about here.

Page 7 of 9 FirstFirst ... 56789 LastLast

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 12:49 PM
  2. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM
  3. Address Book Help
    By clevel211 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 21st, 2010, 09:42 PM
  4. How to get your computer name and IP address?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 6
    Last Post: November 11th, 2010, 05:06 AM
  5. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 03:08 AM