java.lang.nullpointerexception:使用try/catch创建列表时格式化
我有一个客户介绍类:
public class CustomerRepository {
private static final String FILE_PATH = "src/poly/customer/data.txt";
public List<AbstractCustomer> customers;
{
try {
customers = readFiles();
} catch (IOException e) {
e.printStackTrace();
}
}
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
public List<AbstractCustomer> readFiles() throws IOException{
List<AbstractCustomer> result = new ArrayList<>();
List<String> lines = Files.readAllLines(Path.of(FILE_PATH));
for (String line : lines) {
String[] parts = line.split(";");
int points = Integer.parseInt(parts[3]);
LocalDate date = LocalDate.parse(parts[4], formatter);
if (parts[0].equals("REGULAR")) {
RegularCustomer customer = new RegularCustomer(parts[1], parts[2], points, date);
result.add(customer);
} else if (parts[0].equals("GOLD")) {
GoldCustomer customer = new GoldCustomer(parts[1], parts[2], points);
result.add(customer);
} else {
throw new IOException();
}
}
return result;
}
我想从文件中阅读客户,然后将其添加到客户列表中。我试图这样做:
{
try {
customers = readFiles();
} catch (IOException e) {
e.printStackTrace();
}
}
这给了我java.lang.nullpointerexception:formatter,我该如何修复?此方法应从文本文件中创建对象,然后将它们添加到客户列表中。我的文本文件看起来像这样:
常规; C1; Alice; 0; 2022-03-10
常规; C2; C2; Bob; 0; 2022-01-04
Gold; C3; C3; Carol; Carol; 0;
I have a CustomerRepository class:
public class CustomerRepository {
private static final String FILE_PATH = "src/poly/customer/data.txt";
public List<AbstractCustomer> customers;
{
try {
customers = readFiles();
} catch (IOException e) {
e.printStackTrace();
}
}
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
public List<AbstractCustomer> readFiles() throws IOException{
List<AbstractCustomer> result = new ArrayList<>();
List<String> lines = Files.readAllLines(Path.of(FILE_PATH));
for (String line : lines) {
String[] parts = line.split(";");
int points = Integer.parseInt(parts[3]);
LocalDate date = LocalDate.parse(parts[4], formatter);
if (parts[0].equals("REGULAR")) {
RegularCustomer customer = new RegularCustomer(parts[1], parts[2], points, date);
result.add(customer);
} else if (parts[0].equals("GOLD")) {
GoldCustomer customer = new GoldCustomer(parts[1], parts[2], points);
result.add(customer);
} else {
throw new IOException();
}
}
return result;
}
I want to read customers from the file, and then add them to the customers List. I tried to do it like this :
{
try {
customers = readFiles();
} catch (IOException e) {
e.printStackTrace();
}
}
This gives me java.lang.NullPointerException: formatter, how can I fix this? This method should create objects from the text file and then add them to the customers list. My text file looks like this:
REGULAR;c1;Alice;0;2022-03-10
REGULAR;c2;Bob;0;2022-01-04
GOLD;c3;Carol;0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的实例初始器(带有TR/CATCE语句的块)在
formatter
的字段初始化器之前执行,因此格式>格式>格式>仍然是null。
您可以只需移动
formatter
上方的声明(包括初始化) instace Initializer ...但是我建议通过使用构造函数来简单地理解代码客户
而不是实例初始器块。我还建议您制作
格式
a static 字段 -dateTimeFormatter
无论如何都是不变的,并且无论如何都可以在最终字段中存储,那么为什么为每个实例创建一个新的呢? (这本身可以解决问题,但我仍然建议为了可读性使用普通的构造函数声明。)The problem is that your instance initializer (the block with the tr/catch statement) executes before the field initializer for
formatter
, soformatter
is still null.You could just move the declaration (including the initialization) of
formatter
above the instance initializer... but I would suggest making the code simpler to understand by using a constructor to initializecustomers
instead of the instance initializer block.I'd also recommend making
formatter
a static field -DateTimeFormatter
is immutable and thread-safe anyway, you're storing it in a final field, so why create a new one for each instance? (That would fix the problem in itself, but I'd still suggest using a normal constructor declaration for the sake of readability.)