Java Java Data Structures Efficiency! Using a Map to store Contact Methods

Brian Maimone
PRO
Brian Maimone
Pro Student 1,644 Points

Not sure how to fix NullPointerException error. Appreciate help..

Found this but it's little hard to utilize: reference variable points to object created in heap but when you create a reference variable of type object by default its point to "null" and when you try to call any method on null, try to access any variable on null you will get this null pointer exception.

Read more: http://javarevisited.blogspot.com/2012/06/common-cause-of-javalangnullpointerexce.html#ixzz3d9q3moju

My code:

com/example/model/Contact.java

1 package com.example.model; 2 ​ 3 import java.util.*; 4 import java.util.HashSet; 5 import java.util.Map; 6 import java.util.Set; 7 import java.util.HashMap; 8 ​ 9 public class Contact { 10 private String mFirstName; 11 private String mLastName; 12 private Map<String, String> mContactMethods; 13

14 public Contact(String firstName, String lastName) { 15 mFirstName = firstName; 16 mLastName = lastName; 17 /* This stores contact methods by name 18 * eg: "phone" => "(555) 555-1234" 19 / 20 Map <String, String> mContactMethods = new HashMap<String, String>(); 21 } 22 ​ 23 public void addContactMethod(String method, String value) { 24 // TODO: Add to the contact method map 25 mContactMethods.put(method, value); 26 } 27 /* 28

  • Returns the available contact methods. eg: phone, pager, 29 * 30
  • @return The name of the contact methods that are available 31 / 32 public Set<String> getAvailableContactMethods() { 33 // FIXME: This should return the current contact method names. 34 Set <String> availableContactMethods = mContactMethods.keySet(); 35 // for (mContactMethod.Entry<String, String> entry : mContactMethod.entrySet()) { 36 // getAvailableContactMethods.add(entry); 37 return availableContactMethods; 38 } 39 /* 40
  • Returns the value for the contact method if it exists, 41 * 42
  • @param methodName The name of the contact method to look up. 43
  • @return The name of the contact methods that are available 44 / 45 public String getContactInfo(String methodName) { 46 // FIXME: return the value for the passed in *methodName 47 String valueOfMethod = mContactMethods.get(methodName); 48

49 return valueOfMethod; 50 } 51 ​ 52 public String getFirstName() { 53 return mFirstName; 54 } 55 ​ 56 public String getLastName() { 57 return mLastName; 58 } 59 ​ 60 }

com/example/model/Contact.java
package com.example.model;

import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;

public class Contact {
  private String mFirstName;
  private String mLastName;
  private Map<String, String> mContactMethods;

  public Contact(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
    /* This stores contact methods by name
     * eg:  "phone" => "(555) 555-1234"
     */
   Map <String, String> mContactMethods = new HashMap<String, String>();
  }

  public void addContactMethod(String method, String value) {
    // TODO: Add to the contact method map
        mContactMethods.put(method, value);
  }
  /**
   * Returns the available contact methods.  eg: phone, pager,
   *
   * @return The name of the contact methods that are available
   */
  public Set<String> getAvailableContactMethods() {
    // FIXME: This should return the current contact method names.
    Set <String> availableContactMethods = mContactMethods.keySet();
   // for (mContactMethod.Entry<String, String> entry : mContactMethod.entrySet()) {
     // getAvailableContactMethods.add(entry);
        return availableContactMethods;
  }
  /**
   * Returns the value for the contact method if it exists, 
   *
   * @param methodName  The name of the contact method to look up.
   * @return The name of the contact methods that are available
   */
  public String getContactInfo(String methodName) {
    // FIXME: return the value for the passed in *methodName*
   String valueOfMethod = mContactMethods.get(methodName); 

    return valueOfMethod;
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
    return mLastName;
  }

}

2 Answers

Brian Maimone
PRO
Brian Maimone
Pro Student 1,644 Points

Thanks Seth. Could you explain a bit more on how the local variable will disappear when the constructor returns and the member variable mContactMethods is left unset.

Seth Kroger
MOD
Seth Kroger
Treehouse Moderator 56,217 Points

In the constructor you have the line

Map <String, String> mContactMethods = new HashMap<String, String>();

Because you're declaring mContactMethods to be a Map inside the constructor you're making a local variable that will override the member variable. The local variable will disappear when the constructor returns and the member variable mContactMethods is left unset.