繁体   English   中英

无法读取数组长度,因为“<local1> " 是 null</local1>

[英]Cannot read the array length because "<local1>" is null

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

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();
            }
        }

    }

}

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

出现异常Cannot read the array length because "<local3>" is null

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

我不明白<local1> (有时是<local3> )来自哪里,这是什么意思?

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

编辑(回答评论) 这个问题中唯一有趣的问题是什么是<local1>我的回答回答了这个问题: <local1>只是一个由File.listFiles()方法创建的数组。 由于路径错误,数组为null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM