gpt4 book ai didi

java - 空指针访问: The variable arrayLocations can only be null at this location

转载 作者:行者123 更新时间:2023-12-01 17:38:43 25 4
gpt4 key购买 nike

所以我试图读入一个文件并从中获取部分内容,但我在第 行不断收到上述错误

arrayLocations[i] = new Location(Double.parseDouble(xArray[1]), Double.parseDouble(xArray[2]))

    int total;
BufferedReader bfr;
String lineObtained = null;
Location[] arrayLocations = null;

try {

bfr = Files.newBufferedReader(path);
lineObtained = bfr.readLine();

} catch (IOException e) {

e.printStackTrace();
return null;
}

String split = lineObtained.split("POSTAL_OFFICE")[1];
String[] y = split.split(" ");
double xCoord = Double.parseDouble(y[0].trim());
double yCoord = Double.parseDouble(y[1].trim());
Location postOffice = new Location(xCoord, yCoord);

String split1 = lineObtained.split("WORKER_ADDRESS")[1];
String[] y1 = split.split(" ");
double xCoord1 = Double.parseDouble(y1[0].trim());
double yCoord1 = Double.parseDouble(y1[1].trim());
Location home = new Location(xCoord, yCoord);

split = lineObtained.split("POSTAL_ADDRESSES")[1].trim();
String[] splits = split.split("\\r?\\n");

for(int i = 0; i < splits.length; i++) {
String[] xArray = splits[i].split(" ");
arrayLocations[i] = new Location(Double.parseDouble(xArray[1]), Double.parseDouble(xArray[2]));
}

PWPInstance instance = new PWPInstance(total, arrayLocations, postOffice, home, random);
return instance;

最佳答案

您将 arrayLocation 声明为 null 并且从未初始化它。

你可能想要类似的东西

Location[] arrayLocations = new Location[splits.length];

就在循环之前(无需在方法开头声明它,需要时声明它),因为这是您首先使用它的地方,也是您知道数组需要有多大的地方。

关于java - 空指针访问: The variable arrayLocations can only be null at this location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61000666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com