java.lang.nullpointerexception:使用try/catch创建列表时格式化

发布于 2025-01-25 10:23:28 字数 1743 浏览 3 评论 0原文

我有一个客户介绍类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2025-02-01 10:23:28

问题在于您的实例初始器(带有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, so formatter is still null.

how can I fix this?

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 initialize customers 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文