0

New to collections and trying out the below code, I am getting the below excpetion:

Exception in thread "main" java.lang.NullPointerException at org.first.collectionsframework.CollectionsFirsttest.addCountytolist at org.first.collectionsframework.CFTmain.main(CFTmain.java:16)

My classes are as shown below, can some onepoint me to what I am doing wrong:

1st: CollectionsFirsttest

2nd: CFTmain

public class CollectionsFirsttest {

  String name;
  int employeeID;
  String city;
  List<String> county;

/* constructor*/
public CollectionsFirsttest(){
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getEmployeeID() {
    return employeeID;
}
public void setEmployeeID(int employeeID) {
    this.employeeID = employeeID;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}

public void addCountytolist(String x)
{
    this.county.add(x);
    System.out.println("County added is:" +x);
}


}   

2nd: CFTmain

public class CFTmain {

public static void main(String[] args)
{
    CollectionsFirsttest CFTobject1=new CollectionsFirsttest();
    CollectionsFirsttest CFTobject2=new CollectionsFirsttest();
    List<Object> objlist= new ArrayList<Object>();

    CFTobject1.setName("Dummy");
    CFTobject1.setEmployeeID(001);
    CFTobject1.setCity("Wonderland");
    CFTobject1.addCountytolist("cvs1");
    CFTobject1.addCountytolist("cvs2");
    CFTobject1.addCountytolist("cvs3");

    CFTobject2.setName("Pumkin");
    CFTobject2.setEmployeeID(002);
    CFTobject2.setCity("Wonderland");
    CFTobject2.addCountytolist("mtv1");
    CFTobject2.addCountytolist("mtv2");
    CFTobject2.addCountytolist("mtv3");

    objlist.add(CFTobject1);
    objlist.add(CFTobject2);

    display(objlist);

}       
public static  void display(List<Object> l)
{
            System.out.println(" The values in list:"+System.identityHashCode(l));
            for (Object d: l)
            {
System.out.println("the values in the first object  is"+System.identityHashCode(d));
                System.out.println ("The name in"+ d.getClass());
            }
}
}

To some people my error might be pretty simple, but since I am new, hope its eating me up. I am running into error as soon as I am trying to enter county into the List. CFTobject1.addCountytolist("cvs1");

user3040077
  • 71
  • 2
  • 10

3 Answers3

1

You need to initialize the List that is storing the counties. A good place to do this is the constructor of the class. If you are not familiar with the diamond operator you coud write

 county = new ArrayList<String>();

else it is

 county = new ArrayList<>();

You should also think about adding private final to the List declaration.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

You never initialize that list. You've only declared the reference. You need a new ArrayList<>() somewhere and assign that to county.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
0

You need to use an object's constructor initialize everything the object needs before you start using it. Specifically, here you should initialize "county":

public class CollectionsFirsttest {
  List<String> county;
  ...

  public CollectionsFirsttest(){
    county = new ArrayList<String>();
    ...
paulsm4
  • 114,292
  • 17
  • 138
  • 190