我正在尝试学习Java概念。在这里,我正在阅读一个文本文件,并尝试按填充字段的升序对字段进行排序。但是我无法排序,得到NullPointerException。
// File Reading and sending text file fields to POJO class
try (BufferedReader br = Files.newBufferedReader(Paths.get("C:\\Users\\keerti.x\\Documents\\Text.txt"))) {
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if ((line.length() != 0)) {
String[] column = line.split("\\s+");
TextFilePojo file = new TextFilePojo();
file.setId(Integer.parseInt(column[0]));
file.setPopulation(Long.parseLong(column[1]));
file.setCountryName(column[2]);
System.out.println(file.getId() + " " + file.getPopulation() + " " + file.getCountryName());
}
}
}
// creating an arrayList and Sorting according to ascending order of population
ArrayList < TextFilePojo > tl = new ArrayList < TextFilePojo > ();
TextFilePojo[] text = new TextFilePojo[10];
for (int i = 0; i < text.length; i++) {
tl.add(new TextFilePojo(text[i].getId(), text[i].getCountryName(), text[i].getPopulation()));
}
Collections.sort(tl);
for (TextFilePojo pop1: tl) {
System.out.println(pop1.getCountryName() + " " + pop1.getId() + " " + pop1.getPopulation());
}
我希望输出是
Bhutan 114 10000
India 111 100000
Nepal 113 100000
China 112 10000000
但是我收到nullPointerException”