我在將掃描文件中的元素添加到字符串集中時收到NullPointerException。Set.add()NullPointerException
public class ChrisCorpManifests implements Manifests {
Set<String> _employees;
@Override
public Set<String> getEmployees() throws FileNotFoundException {
Scanner s = null;
String string;
try {
s = new Scanner(new BufferedReader(new FileReader("EmployeeManifest.csv")));
s.useDelimiter("\n");
while (s.hasNextLine()) {
string = s.nextLine();
System.out.println(string);
_employees.add(string);
}
} finally {
if (s != null) {
s.close();
}
}
return _employees;
}
我收到了NullPointerException異常在這條線:
_employees.add(string);
爲什麼我可以打印字符串,但不能將其添加到字符串設定?
我假定字符串都是唯一的? –
_employees來自哪裏? – danpaq
'_employees'爲空嗎? –