【问题标题】:Cannot read the array length because "<local1>" is null无法读取数组长度,因为“<local1>”为空
【发布时间】:2021-01-31 15:22:36
【问题描述】:

我正在用 java 制作股票市场模拟器应用程序,deleteHistoryFiles() 方法中存在问题。它说数组为空。但是,我不知道这个错误在说什么数组。 下面是代码(我删除了一些节省空间的方法):

package stock.market.simulator;

import java.util.Random;
import java.text.DecimalFormat;

import java.util.Timer;
import java.util.TimerTask;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class StockMarketSimulator {
    
    // Path to where the files are stored for rate history 
    // USE WHEN RUNNING PROJECT IN NETBEANS
    //public static final String HISTORYFILEPATH = "src/stock/market/simulator/history/";

    // Path to history files to be used when executing program through jar file
    public static final String HISTORYFILEPATH = "history/";
    
    public static void main(String[] args) throws IOException {

        accountProfile accProfile = accountCreation();

        stockProfile[][] stockProfile = createAllStocks();

        deleteHistoryFiles(new File(HISTORYFILEPATH));
        createHistoryFiles(stockProfile);

        mainWindow window = new mainWindow(accProfile, stockProfile);

        recalculationLoop(stockProfile, window);

    }
    // Procedure to create the history files
    public static void createHistoryFiles(stockProfile[][] stocks) throws IOException {

        String fileName;
        FileWriter fileWriter;

        for (stockProfile[] stockArray : stocks) {
            for (stockProfile stock : stockArray) {
                fileName = stock.getProfileName() + ".csv";
                fileWriter = new FileWriter(HISTORYFILEPATH + fileName);
            }
        }

    }

    // Procedure to delete the history files
    public static void deleteHistoryFiles(File directory) {
        for (File file : directory.listFiles()) {
            if (!file.isDirectory()) {
                file.delete();
            }
        }

    }

}

【问题讨论】:

  • 请显示实际错误,包括行号(您应该在代码中指出,因为您删除了部分代码)。

标签: java arrays file nullpointerexception


【解决方案1】:

我在完全相同的场景中遇到了同样的异常。我尝试通过调用File.listFiles() 然后迭代数组来创建一个文件数组。

遇到异常Cannot read the array length because "&lt;local3&gt;" is null

问题是该目录的路径根本不存在(我的代码是从另一台具有不同文件夹结构的机器上复制的)。

我不明白&lt;local1&gt;(有时是&lt;local3&gt;)来自哪里,是什么意思?

应该是这样的:Cannot read the array length because the array is null

【讨论】:

  • 这是一个答案吗?它读起来更像是“我也有这个问题”的帖子。您能否请edit您的帖子澄清为什么这会回答这个问题?如果不是答案,请将其删除并在问题上发表评论。
猜你喜欢
  • 2021-04-20
  • 1970-01-01
  • 2021-11-26
  • 2014-07-31
  • 2014-10-12
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多