1 Replies - 519 Views - Last Post: 23 April 2012 - 06:29 AM Rate Topic: -----

#1 ca7747  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-October 11

binary search null pointer exception

Posted 23 April 2012 - 05:59 AM

The binary searches(in the TestContact class) by name and email keep returning a null pointer exception. I cannot change the search method, I have to use binary. If anyone can tell me what's wrong with the code that'd be great. I really don't want any code given to me though, I'd like to figure that out myself. Thanks in advance.

public class Contact
{
    private String name, relation, bday, phone, email;

    public Contact(String name, String relation, String bday, String phone, String email)
    {
        this.name = name;
        this.relation = relation;
        this.bday = bday;
        this.phone = phone;
        this.email = email;
    }

    public String getName()
    {
        return name;
    }
    
    public String getRelation()
    {
        return relation;
    }
    
    public String getBday()
    {
        return bday;
    }
    
    public String getPhone()
    {
        return phone;
    }
    
    public String getEmail()
    {
        return email;
    }
    
    public String toString()
    {
        return name + "\t" + relation + "\t\t" + bday + "\t\t" + phone + "\t" +email;
    }
}



public class TestContact
{
    public static void main(String [] args)
    {
        Contact [] myContacts = new Contact[6];
        myContacts[0] = new Contact("John Carter", "brother", "Mar 3", "(342)555-7069", "jcarter@carter.com");
        myContacts[1] = new Contact("Elise Carter", "mom", "Apr 19", "(342)555-7011", "carterMom@carter.com");
        myContacts[2] = new Contact("Ellie Carter", "me", "Jun 10", "(342)555-8102", "ecarter@carter.com");
        myContacts[3] = new Contact("Sue Ellen", "friend", "Mar 9", "(341)555-9182", "susieE@hotmail.com");
        myContacts[4] = new Contact("Frank Carter", "dad", "Dec 1", "(342)555-7011", "carterDad@carter.com");
        myContacts[5] = new Contact("Johnnie\t", "friend", "Jan 21", "(341)555-7789", "jDawg5555@yahoo.com");
        
        System.out.printf("%50s%n", "Contact List");
        printContacts(myContacts);
        System.out.println();
        
        System.out.println("Find Name - Johnnie");
        findByName(myContacts, "Johnnie\t");
        System.out.println();
        System.out.println("Find Name - Sam Parker");
        findByName(myContacts, "Sam Parker");
        System.out.println();
        
        System.out.println("Find Relation - Friend");
        findByRelation(myContacts, "friend");
        System.out.println();
        System.out.println("Find Relation - Aunt");
        findByRelation(myContacts, "aunt");
        System.out.println();
        
        System.out.println("Find Bday - May");
        findByMonth(myContacts, "May");
        System.out.println();
        System.out.println("Find Bday - Mar");
        findByMonth(myContacts, "Mar");
        System.out.println();
        
        System.out.println("Find Phone - (333)555-8989");
        findByPhone(myContacts, "(333)555-8989");
        System.out.println();
        System.out.println("Find Phone - (342)555-7011");
        findByPhone(myContacts, "(342)555-7011");
        System.out.println();
        
        System.out.println("Find Email - rgoodman@gotmail.com");
        findByEmail(myContacts, "rgoodman@gotmail.com");
        System.out.println();
        System.out.println("Find Email - susieE@hotmail.com");
        findByEmail(myContacts, "susieE@hotmail.com");
        System.out.println();
    }
    
    public static void printContacts(Contact [] myContacts)
    {
        System.out.println("Name" + "\t\t" + "Relation" + "\t" + "Birthday" + "\t" + "Phone" + "\t\t" + "Email");
        System.out.println("-------------------------------------------------------------------------------------------");
        for(int x = 0; x < myContacts.length; x++)
              System.out.println(myContacts[x]);
    }
    
    public static void findByName(Contact [] myContacts, String name)
    {
        Contact [] arr = insertionSortName(myContacts); 
        int low = -1;    
        int high = myContacts.length;
        int probe;
        int found = -1;
        while(high - low > 1)
        {
            probe = (high + low) /2;
            if(arr[probe].getName().compareTo(name) > 0)
            high = probe;
            else 
            low = probe;
            if((low >= 0) && (arr[low].getName().compareTo(name) == 0))
            found = low;
            else
            found = -1;
        }
        
        if(found != -1)
           System.out.println("Found: " + arr[found]);
        else
            System.out.println("Not found.");
    }
    
    
     public static Contact [] insertionSortName(Contact [] myContacts)
    {
         Contact [] arr = new Contact[myContacts.length];
        for (int outer = 1; outer < myContacts.length - 1; outer++) 
                { 
                    int pos = outer; 
                    Contact temp= myContacts[outer];  
                    int insertindex = 0;
                    
                    while (pos>0 && insertindex == 0) 
                    { 
                        if((temp.getName().compareTo(myContacts[pos-1].getName()) > 0 ))
                        {
                            insertindex = pos;
                        }
                        else
                        {
                            arr[pos] = arr[pos-1]; 
                        }
                        pos--; 
                    } 
                    arr[insertindex] = temp; 
                } 
              return arr;
    }
    
    public static void findByRelation(Contact [] myContacts, String relation)
    {
        String found = "";
        int count = 0;
        System.out.println("Find Results: ");
        for(int i = 0; i < myContacts.length; i++)
         {
             if (myContacts[i].getRelation().compareTo(relation) == 0)
              {
                  found += (myContacts[i] + "\n");
                  count++;
                }
                
        }
        if(count == 0)
            found = "Not found. \n";
            
    System.out.println(found + "There were " + count + " listings for " + relation);
}

 public static void findByMonth(Contact [] myContacts, String bdayMonth)
    {
        String found = "";
        int count = 0;
        System.out.println("Find Results: ");
        for(int i = 0; i < myContacts.length; i++)
         {
             if ((myContacts[i].getBday().substring(0,3)).compareTo(bdayMonth) == 0)
              {
                  found += (myContacts[i] + "\n");
                  count++;
                }
                
        }
        if(count == 0)
            found = "Not found. \n";
            
    System.out.println(found + "There were " + count + " listings for " + bdayMonth);
}

 public static void findByPhone(Contact [] myContacts, String phone)
    {
        String found = "";
        int count = 0;
        System.out.println("Find Results: ");
        for(int i = 0; i < myContacts.length; i++)
         {
             if ((myContacts[i].getPhone().compareTo(phone)) == 0)
              {
                  found += (myContacts[i] + "\n");
                  count++;
                }
                
        }
        if(count == 0)
            found = "Not found. \n";
            
    System.out.println(found + "There were " + count + " listings for " + phone);
}

public static void findByEmail(Contact [] myContacts, String email)
    {
        Contact[] dest = insertionSortEmail(myContacts); 
        int low = -1;    
        int high = myContacts.length;
        int probe;
        int found = -1;
        while(high - low > 1)
        {
            probe = (high + low) /2;
            if(dest[probe].getEmail().compareTo(email) > 0)
            high = probe;
            else 
            low = probe;
            if((low >= 0) && (dest[low].getEmail().compareTo(email) == 0))
            found = low;
            else
            found = -1;
        }
        
        if(found != -1)
           System.out.println("Found: " + dest[found]);
        else
            System.out.println("Not found.");
        }
    
    
     public static Contact [] insertionSortEmail(Contact [] myContacts)
    {
         Contact[] dest = new Contact[myContacts.length];
        for (int outer = 1; outer < myContacts.length; outer++) 
                { 
                    int pos = outer; 
                    Contact temp= myContacts[outer]; 
                    int insertindex = 0;

                    while (pos>0 && insertindex == 0) 
                    { 
                        if((temp.getEmail().compareTo(myContacts[pos-1].getEmail()) > 0 ))
                        {
                            insertindex = pos;
                        }
                        else
                        {
                            dest[pos] = dest[pos-1]; 
                        }
                        pos--; 
                    } 
                    dest[insertindex] = temp; 
                } 
              return dest;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: binary search null pointer exception

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: binary search null pointer exception

Posted 23 April 2012 - 06:29 AM

Your best bet to solve this problem by yourself (which is the best way of doing things) is to set a breakpoint in your search method and step through until the exception is thrown. You can then see what object is null, why its null and probably how to fix it as well.

Depending on what IDE you are using setting breakpoints might be slightly different (not much).

Try searching Google for some Debugging Tutorials/Info etc with your IDE.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1