Chris Keli:
Donc, je suis en train de lire dans un fichier et obtenir des pièces de celui-ci, mais je continue à obtenir l'erreur ci-dessus à la ligne
arrayLocations [i] = new Lieu de travail (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;
Federico Klez Culloca:
Vous avez déclaré arrayLocation
que null
jamais initialisés.
Vous voulez probablement quelque chose comme
Location[] arrayLocations = new Location[splits.length];
Juste avant que la boucle (pas besoin de le déclarer au début de la méthode, le déclarer quand il est nécessaire), puisque c'est le premier endroit où vous l'utilisez et l'endroit où vous savez comment les grands besoins de tableau à.