java - Null exception on array -


i'm new java , programming , working on class project, we're learning i/o, arrays , objects , have pretty specific guidelines follow. when compiler gets "countryinfo[count].setname(name);" , gives me

exception in thread "main" java.lang.nullpointerexception @ main.main(main.java:53)

if comment out, next line gives me same error. i'm sure there's more efficient ways re write code, since we're newbies aren't allowed that. read quite bit null exception before asked... if missed somewhere apologize. i'm lost :(

public class main {  /**  * @param args command line arguments  */ private static country[] countryinfo = new country[43];  public static void main(string[] args) throws ioexception {     string name = "";     string capital = "";     string region = "";     int region_nbr = 0;     int capital_population = 0;      // todo code application logic here     string filename = "countries.txt";     string inputstring;      fileinputstream fis1 = new fileinputstream(filename);     bufferedreader br1 = new bufferedreader(new inputstreamreader(fis1));     inputstring = br1.readline();      while (inputstring != null) {         int count = 0;         system.out.print(inputstring + "\n");          name = inputstring.substring(0, 13).trim();         //system.out.print(name + ", "); //echo          capital = inputstring.substring(24, 36).trim();         //system.out.print(capital + ", ");//echo          region = inputstring.substring(40, 56).trim();         //system.out.print(region + ", "); //echo          region_nbr = integer.parseint(inputstring.substring(64, 66).trim());         //system.out.print(region_nbr + ", ");//echo          capital_population = integer.parseint(inputstring.substring(72, inputstring.length()).trim());         //system.out.print(capital_population + "\n");          countryinfo[count].setname(name);         countryinfo[count].setcapital(capital);         countryinfo[count].setregion(region);         countryinfo[count].setregionnum(region_nbr);         countryinfo[count].setpopulation(capital_population);         inputstring = br1.readline();          count++;     } //end while  } 

}

public class country {  private string name; private string capital; private string region; private int region_nbr; private int capital_population;    public void setname(string w) {     this.name = w;  } //end name 

you should add:

countryinfo[count] = new country(); 

before

countryinfo[count].setname(name); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -