Acceso puntero nulo: Los arrayLocations variable sólo puede ser nulo en esta ubicación

Chris Keli:

Así que estoy tratando de leer en un archivo y obtener partes de ella, pero me siguen dando el error anterior en la línea

arrayLocations [i] = new Ubicación (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:

Usted declaró arrayLocationque nullnunca se ha inicializado.

Es posible que desee algo así como

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

Justo antes de que el bucle (sin necesidad de declarar que al inicio del procedimiento, declarar que cuando es necesario), ya que es el primer lugar que lo usa y el lugar que sabe lo grande que las necesidades de matriz que sean.

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=401014&siteId=1
Recomendado
Clasificación