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");