Meta Programming
Meta Programming

Reputation: 11

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

I am making a stock market simulator app in java, and there is an issue in the deleteHistoryFiles() method. It says that array is null. However, I have no idea what array this error is talking about. Here's the code (I've deleted some methods to save space):

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

    }

}

Upvotes: 0

Views: 12353

Answers (1)

Yuriy N.
Yuriy N.

Reputation: 6127

I got the same exception in exactly the same scenario. I tried to create an array of files by calling File.listFiles() and then iterating the array.

Got exception Cannot read the array length because "<local3>" is null.

Problem is that the path to the directory simply does not exist (my code was copied from another machine with a different folder structure).

I don't understand where is <local1> (sometimes it is <local3>) comes from and what does it mean?

It should be just like this: Cannot read the array length because the array is null.

Edit (answering comment) The sole interesting question in this question is what is a <local1> My answer answers this question: <local1> is just an array created by File.listFiles() method. And an array is null because of the wrong path.

Upvotes: 0

Related Questions