GraphHopper.com | Forum | GitHub | Maps | Blog

Java.lang.NullPointerException: null when creating Algorithm


#1

Hello guys,
I get an NPE with the following message when my code attempts to create the algorithm (I’m using Kotlin btw)

val vehicle= createVehicleBuilder(createVehicleType())?.build()
    val costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true)
    val destinations = populateDestinations(nodes)
    val costMatrix = createCostMatrix(distanceMatrix, timeMatrix, costMatrixBuilder)

    var problem = VehicleRoutingProblem
            .Builder
            .newInstance()
            .setRoutingCost(costMatrix)
            .addVehicle(vehicle)
            .addAllJobs(destinations)
            .setFleetSize(VehicleRoutingProblem.FleetSize.INFINITE)
            .build()

    var algorithm = Jsprit.createAlgorithm(problem)

and this is the createCostMatrix function:

distanceMatrix.forEachIndexed { i, row ->
        row.forEachIndexed { j, value ->
            costMatrixBuilder.addTransportDistance("$i", "$j", value)
        }
    }

    timeMatrix.forEachIndexed { i, row ->
        row.forEachIndexed { j, value ->
            costMatrixBuilder.addTransportTime("$i", "$j", value)
        }
    }

    return costMatrixBuilder.build()

and finally, populateDestinations:
var destinations = ArrayList()

    nodi.forEachIndexed { index, nodo ->
        val servizio = Service
                .Builder
                .newInstance(index.toString())
                .setName(nodo.indirizzo)
                .addSizeDimension(0, 1)
                .setLocation(Location.newInstance("${index + 1}"))

        if (nodo.haOrari)
            servizio.setTimeWindow(TimeWindow.newInstance(nodo.oraInizioMinuti!!, nodo.oraFineMinuti!!))

        destinations.add(servizio.build())
    }

    return destinations;

And this is the

java.lang.NullPointerException: null
at com.graphhopper.jsprit.core.util.EuclideanDistanceCalculator.calculateDistance(EuclideanDistanceCalculator.java:24) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.ruin.distance.AvgServiceAndShipmentDistance.calcDist(AvgServiceAndShipmentDistance.java:91) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.ruin.distance.AvgServiceAndShipmentDistance.calcDist(AvgServiceAndShipmentDistance.java:68) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.ruin.distance.AvgServiceAndShipmentDistance.getDistance(AvgServiceAndShipmentDistance.java:55) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.ruin.JobNeighborhoodsOptimized.calculateDistancesFromJob2Job(JobNeighborhoodsOptimized.java:123) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.ruin.JobNeighborhoodsOptimized.initialise(JobNeighborhoodsOptimized.java:106) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.box.Jsprit.create(Jsprit.java:436) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.box.Jsprit.access$100(Jsprit.java:54) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.box.Jsprit$Builder.buildAlgorithm(Jsprit.java:308) ~[jsprit-core-1.7.2.jar:na]
at com.graphhopper.jsprit.core.algorithm.box.Jsprit.createAlgorithm(Jsprit.java:143) ~[jsprit-core-1.7.2.jar:na]


#2

I would suggest setting a breakpoint in the offending line and looking at what’s going on there