Hey can someone please help me understand why I keep getting a null pointer exception while trying to run this? I was just trying to learn arraylist for a course I am taking.
////////////////////////////////////////////
public class Address {
int houseNumber;
String roadNumber;
String areaName;
String cityName;
public Address(int hn, String rn, String an, String cn){
this.houseNumber = hn;
this.roadNumber = rn;
this.areaName = an;
this.cityName = cn;
}
public String toString() {
return "House # " + this.houseNumber + "\nRoad # " + this.roadNumber + "\n" + this.areaName + "\n" + this.cityName;
}
}
/////////////////////////////////////////////////
import java.util.ArrayList;
public class AddressReferences {
ArrayList<Address> myCollection = new ArrayList<Address>();
public void addAddress (int hn, String rn, String an, String cn) {
myCollection.add(new Address(hn,rn,an,cn));
}
public void printAddress () {
for ( Address i : myCollection){
System.out.println (i);
}
}
}
//////////////////////////////////////
public class Test {
static AddressReferences ar;
public static void main(String[] args){
ar.addAddress(46, "9/a", "Kotol", "Dhaka");
ar.addAddress(44, "9/a", "Kotol", "Dhaka");
ar.addAddress(28, "9/a", "Kotol", "Dhaka");
ar.addAddress(89, "12/a", "Kotol", "Dhaka");
ar.addAddress(60, "7/a", "Kotol", "Dhaka");
ar.printAddress();
}
}