5 Replies - 712 Views - Last Post: 09 April 2013 - 02:24 AM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

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

Why does my method keep returning null?

Posted 09 April 2013 - 12:54 AM

I have a Contact object, and a list of Contact objects. My method is supposed to take a String, which will be the last name of one of the Contact objects, then find and return that Contact. Or return null if the Contact was not found, however for some reason it keeps returning null every time, even if the Contact is in the list. These are the two methods that I wrote, the first one is just to make the second one easier. I cannot figure out why it keeps returning null, it looks fine to me.


/**
   * 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;
      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;       
  }
  
  /**
   * Finds and returns a Contact object by last name
   * @param lstnm The last name of the Contact that needs to be found 
   * @return contactToBeFound The Contact that was found
   */
  public Contact find(String lstnm)
  {
      Contact contactToBeFound; 
      int contactIndex = findContactIndex(lstnm); // Gets the index value of the Contact
      if(contactIndex == -1)
      {
          contactToBeFound = null;
      }
      else
      {
          contactToBeFound = Contact_List[contactIndex]; // Assigns the contact being removed
      }
      return contactToBeFound;
  }




Is This A Good Question/Topic? 0
  • +

Replies To: Why does my method keep returning null?

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Why does my method keep returning null?

Posted 09 April 2013 - 01:32 AM

There are two ways I can see for your method to return null:

1. If it does not find the item in the array.
2. If the item it finds is null (i.e. null is stored in the array)

Number 2 shouldn't ever happen because the helper method would throw an exception. That leaves number 1.

If the array is empty then it wouldn't find the item.
If the value of numContacts is wrong then it might not reach the item you are looking for.
If the item actually isn't in the array then it would not find it.

You could try adding some System.out.println() calls to your program to inspect the values of the variables. That might help you decide which (if any) of my theories are correct.
Was This Post Helpful? 1
  • +
  • -

#3 ladyaoi  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 87
  • Joined: 22-September 11

Re: Why does my method keep returning null?

Posted 09 April 2013 - 01:33 AM

I'm still pretty new to java, so I may not have the right answer at a glance.

But this is what I see...

You have a method that returns a value for indexOfContact. In that method, at one point you expressly code
indexOfContact = -1

Your next method declares a variable equal to the first method, which returns indexOfContact.

In one of two places a value isn't being passed the way you think is should be.

Either the first method is not passing the new value of indexOfContact from the the For-Loop
Or
Passing a method as a value is not working

I would try moving the declaration for indexOfContact outside the method.
Then you can remove this line
 int contactIndex = findContactIndex(lstnm);
and rewrite your if statement to
 if (indexOfContact == -1) 

Was This Post Helpful? 0
  • +
  • -

#4 Stephenfrazier10  Icon User is offline

  • D.I.C Head

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

Re: Why does my method keep returning null?

Posted 09 April 2013 - 01:41 AM

cfoley, you were right with the numContacts being wrong. When I redid my add Contact method, I forgot to put in the numContacts++, so that was always at 0.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Why does my method keep returning null?

Posted 09 April 2013 - 01:48 AM

Cool. :-) Instead of having to track it yourself, use Contact_List.length
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Why does my method keep returning null?

Posted 09 April 2013 - 02:24 AM

Sorry, that last piece of advice might not be good. It's exactly what to do if your array is exactly the right size for the amount of data. However, if you don't know how much data you have, your array might be longer in which case you really do need to keep track of it yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1