-2

I need to make a list of countries based on continets. I have made a list of countries, based on a class (in my case, Europe- that has String Name, int population, int surface- separate class with getters, setters and constructor.) Now I've made 3 lists of name, population, and surface, all with the same number of indexes., using the Arrays.asList method, as follows:

List<String> countriesEurope = Arrays.asList("Russia", "Germany", "United Kingdom", "France", "Italy", "Spain", "Ukraine", "Poland", "Romania", "Netherlands", "Belgium", "Czech Republic", "Greece", "Portugal", "Sweden", "Hungary", "Belarus", "Austria", "Serbia", "Switzerland", "Bulgaria", "Denmark", "Finland", "Slovakia", "Norway", "Ireland", "Croatia", "Moldova", "Bosnia and Herzegovina", "Albania", "Lithuania", "North Macedonia", "Slovenia", "Latvia", "Estonia", "Montenegro", "Luxembourg", "Malta", "Iceland", "Channel Islands", "Isle of Man", "Andorra", "Faeroe Islands", "Monaco", "Liechtenstein", "San Marino", "Gibraltar", "Holy See");
List<Integer> populationEurope = Arrays.asList(145934462,83783942,67886011,65273511,60461826,46754778,43733762,37846611,19237691, 17134872, 11589623, 10708981, 10423054, 10196709, 10099265, 9660351, 9449323, 9006398, 8737371, 8654622, 6948445, 5792202, 5540720, 5459642, 5421241, 4937786, 4105267, 4033963, 3280819, 2877797, 2722289, 2083374, 2078938, 1886198, 1326535, 628066, 625978, 441543, 341243, 173863, 85033, 77265, 48863, 39242, 38128, 33931, 33691, 801);
List<Integer> surfaceEurope = Arrays.asList(16376870,34856,24193,547557,29414,4988,57932,30623,23017,3372,3028,7724,1289, 9159, 41034, 9053, 20291, 82409, 8746, 39516, 10856, 4243, 30389, 48088, 365268, 6889, 5596, 3285, 51, 274, 62674, 2522, 2014, 622, 4239, 1345, 259, 320, 10025, 190, 570, 470, 1396, 1, 160, 60, 10, 0);

I want to add them to a new list, that will contain all this data, with the following method:

List<Europe> europeList ;

for (int i=0; i<countriesEurope.size(); i++){
    europeList.add(new Europe(countriesEurope.get(i), populationEurope.get(i), surfaceEurope.get(i)));
}

When I want to print, using sysout, I can not print them, as I get:

xception in thread "main" java.lang.NullPointerException
    at AllCountries.main(AllCountries.java:19)

1 Answers1

0

Sounds like you forgot to actually make a list here. europeList is a reference variable - it's like a blank page in an addressbook. You gotta build a house first:

List<Europe> europeList = new ArrayList<Europe>();

Now you can call europeList.add without getting an NPE.

. is java-ese for: Walk over to this address and do something with the house you find there. NullPointerException is: '.uhh.. the page is blank; I can't walk over to the place you want me to walk to'.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for that. That was it, only one problem now, only the names are added, population and surface is not shown now, using: ```List europeList = new ArrayList() ; for (int i=0; i – theadvocate Dec 14 '22 at 16:56
  • Sounds like a broken toString implementation; it has nothing to do with this question, so ask a separate one. Or, more likely, search for the answer; it's extremely unlikely your question wouldn't have been asked before – rzwitserloot Dec 14 '22 at 21:58