2 Replies - 190 Views - Last Post: 05 April 2013 - 03:50 AM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Keep getting a Null Error with Add method

Posted 05 April 2013 - 12:08 AM

I have an array setup called a Contact_List, and the add method is supposed to place the new Contact in the list based on its last name in alphabetical order. I have been trying this for hours and cannot figure it out. I have the add method below, and the findContactIndex method which the add method calls on.

/**
   * Adds a new Contact to the Contact_List
   * @param frnd The new Contact Object to be added
   */
  public void add(Contact frnd) 
  {
      if(numContacts == 0)
      {
          Contact_List[0] = frnd;
      }
      else
      {
          for(int i = 0; i < numContacts; i++)
          {
              int x = frnd.getLastName().compareToIgnoreCase(Contact_List[i].getLastName());
              if(x > 0)
              {
                  for(int a = findContactIndex(Contact_List[i].getLastName()); a < numContacts; a++)
                  {
                      Contact_List[a] = Contact_List[a + 1]; // Shift all of the Contacts down
                  }
                  Contact_List[findContactIndex(Contact_List[i].getLastName()) - 1] = frnd;
                  break;
              }
          }
      }
      numContacts++;
  }
  
  
  /**
   * Searches the Contact_List to find a contact by last name
   * @param lstnm The last name of a Contact to be found
   * @return indexOfContact The index of the contact that has been found
   */
  public int findContactIndex(String lstnm)
  {
      int indexOfContact = -1; // Defaults it at -1 incase the name is not found
      for(int i = 0; i < numContacts; i++) // Once for every Contact
      {
          int a = lstnm.compareToIgnoreCase(Contact_List[i].getLastName());
          if( a == 0) // If the Contact is found
          {
              indexOfContact = i; // Assign the index of that Contact
          }
      }
      return indexOfContact;       
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Keep getting a Null Error with Add method

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Keep getting a Null Error with Add method

Posted 05 April 2013 - 03:46 AM

Post the exact error/stack trace. Be sure to include the code pointed at by the error/stack trace. Since line numbers may not match, provide the translation from the error/stack trace to the posted code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Keep getting a Null Error with Add method

Posted 05 April 2013 - 03:50 AM

First perform this operation only once: frnd.getLastName()
If there is 10 thousand elements in your array, no need to call that method 10 thousand times so store int in a temp variable

String lastName = frnd.getLastName();

You shifting down is wrong and it should start by the end or you will simply fill your array with the same contact
And no need to call findIndex() you already have it and probably that the method won't work as the contact is not in the list yet :)
   for(int x = numContacts + 1; --x > i;)
       contact_list[x] = contact_list[x-1];
   contact_list[i] = frnd;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1