matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
84 stars 179 forks source link

java.lang.NullPointerException while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory #520

Open vsgo222 opened 3 years ago

vsgo222 commented 3 years ago

Hello all, This is my extension to my previously closed post : https://github.com/matsim-org/matsim-code-examples/issues/515#issue-792991933. I modified the network and transitSchedule.xml to introduce exclusive bus lane on some of the pre-identified routes. But I am now getting null pointer exception.

2021-02-03T10:19:10,514  WARN PrepareForSimImpl:222 'usePersonIdForMissingVehicleId' is deprecated. It will be removed soon.
2021-02-03T10:19:36,654 ERROR MatsimRuntimeModifications:75 Getting uncaught Exception in Thread main
com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.lang.NullPointerException
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptor
    for the 1st parameter of ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorRoutingModuleProvider.<init>(SwissRailRaptorRoutingModuleProvider.java:28)
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorRoutingModuleProvider
  while locating org.matsim.core.router.RoutingModule annotated with @com.google.inject.name.Named(value="bus")
  while locating org.matsim.core.router.RoutingModule annotated with @com.google.inject.internal.Element(setName=,uniqueId=114, type=MAPBINDER, keyType=java.lang.String)
  at org.matsim.core.router.TripRouter.<init>(TripRouter.java:102)
  while locating org.matsim.core.router.TripRouter

1 error
    at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053) ~[guice-4.2.2.jar:?]
    at org.matsim.core.controler.PrepareForSimImpl.lambda$0(PrepareForSimImpl.java:173) ~[classes/:?]
    at org.matsim.core.population.algorithms.ParallelPersonAlgorithmUtils.run(ParallelPersonAlgorithmUtils.java:86) ~[classes/:?]
    at org.matsim.core.controler.PrepareForSimImpl.run(PrepareForSimImpl.java:172) ~[classes/:?]
    at org.matsim.core.controler.NewControler.prepareForSim(NewControler.java:113) ~[classes/:?]
    at org.matsim.core.controler.AbstractController$1.run(AbstractController.java:80) ~[classes/:?]
    at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:69) [classes/:?]
    at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:52) [classes/:?]
    at org.matsim.core.controler.AbstractController.run(AbstractController.java:89) [classes/:?]
    at org.matsim.core.controler.NewControler.run(NewControler.java:82) [classes/:?]
    at org.matsim.core.controler.Controler.run(Controler.java:249) [classes/:?]
    at org.matsim.run.Controler.run(Controler.java:59) [classes/:?]
    at org.matsim.run.Controler.main(Controler.java:63) [classes/:?]
Caused by: java.lang.NullPointerException
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.calculateRouteStopTransfers(SwissRailRaptorData.java:256) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.create(SwissRailRaptorData.java:186) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.prepareData(SwissRailRaptorFactory.java:77) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.getData(SwissRailRaptorFactory.java:66) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.get(SwissRailRaptorFactory.java:60) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.get(SwissRailRaptorFactory.java:1) ~[classes/:?]
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:85) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:77) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:59) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:61) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:42) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:65) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:113) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:60) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:62) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050) ~[guice-4.2.2.jar:?]
    at org.matsim.core.router.TripRouter.<init>(TripRouter.java:107) ~[classes/:?]
    at org.matsim.core.router.TripRouter$$FastClassByGuice$$40b051f1.newInstance(<generated>) ~[classes/:?]
    at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050) ~[guice-4.2.2.jar:?]
    ... 12 more
2021-02-03T10:19:36,690 ERROR MatsimRuntimeModifications:81 ERROR --- This is an unexpected shutdown!
2021-02-03T10:19:36,690 ERROR MatsimRuntimeModifications:84 Shutdown possibly caused by the following Exception:
com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.lang.NullPointerException
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptor
    for the 1st parameter of ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorRoutingModuleProvider.<init>(SwissRailRaptorRoutingModuleProvider.java:28)
  while locating ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorRoutingModuleProvider
  while locating org.matsim.core.router.RoutingModule annotated with @com.google.inject.name.Named(value="bus")
  while locating org.matsim.core.router.RoutingModule annotated with @com.google.inject.internal.Element(setName=,uniqueId=114, type=MAPBINDER, keyType=java.lang.String)
  at org.matsim.core.router.TripRouter.<init>(TripRouter.java:102)
  while locating org.matsim.core.router.TripRouter

1 error
    at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053) ~[guice-4.2.2.jar:?]
    at org.matsim.core.controler.PrepareForSimImpl.lambda$0(PrepareForSimImpl.java:173) ~[classes/:?]
    at org.matsim.core.population.algorithms.ParallelPersonAlgorithmUtils.run(ParallelPersonAlgorithmUtils.java:86) ~[classes/:?]
    at org.matsim.core.controler.PrepareForSimImpl.run(PrepareForSimImpl.java:172) ~[classes/:?]
    at org.matsim.core.controler.NewControler.prepareForSim(NewControler.java:113) ~[classes/:?]
    at org.matsim.core.controler.AbstractController$1.run(AbstractController.java:80) ~[classes/:?]
    at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:69) [classes/:?]
    at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:52) [classes/:?]
    at org.matsim.core.controler.AbstractController.run(AbstractController.java:89) [classes/:?]
    at org.matsim.core.controler.NewControler.run(NewControler.java:82) [classes/:?]
    at org.matsim.core.controler.Controler.run(Controler.java:249) [classes/:?]
    at org.matsim.run.Controler.run(Controler.java:59) [classes/:?]
    at org.matsim.run.Controler.main(Controler.java:63) [classes/:?]
Caused by: java.lang.NullPointerException
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.calculateRouteStopTransfers(SwissRailRaptorData.java:256) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.create(SwissRailRaptorData.java:186) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.prepareData(SwissRailRaptorFactory.java:77) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.getData(SwissRailRaptorFactory.java:66) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.get(SwissRailRaptorFactory.java:60) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.get(SwissRailRaptorFactory.java:1) ~[classes/:?]
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:85) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:77) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:59) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:61) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:42) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:65) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:113) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:60) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:62) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050) ~[guice-4.2.2.jar:?]
    at org.matsim.core.router.TripRouter.<init>(TripRouter.java:107) ~[classes/:?]
    at org.matsim.core.router.TripRouter$$FastClassByGuice$$40b051f1.newInstance(<generated>) ~[classes/:?]
    at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306) ~[guice-4.2.2.jar:?]
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050) ~[guice-4.2.2.jar:?]
    ... 12 more
2021-02-03T10:19:46,104 ERROR MatsimRuntimeModifications:99 ERROR --- MATSim unexpectedly terminated. Please check the output or the logfile with warnings and errors for hints.
2021-02-03T10:19:46,105 ERROR MatsimRuntimeModifications:100 ERROR --- results should not be used for further analysis.
2021-02-03T10:19:46,105 ERROR MatsimRuntimeModifications:104 ERROR --- This was an unexpected shutdown! See the log file for a possible reason.

I looked into the lines where the code triggers i.e. (SwissRailRaptorRoutingModuleProvider.java:28). Unfortunately, the line corresponds to a comment. So, I am guessing that the "error" is in the public class constructor file which is attempted to initialized. there are few functions

Not sure, I am digging things in the right direction.

Any help, direction on resolving this issue would be very much appreciated. output_HighPTRdrshp_BusLane_v5.zip

Thank you Vedant

mrieser commented 3 years ago

The log contains additional information, especially these lines:

Caused by: java.lang.NullPointerException
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.calculateRouteStopTransfers(SwissRailRaptorData.java:256) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.create(SwissRailRaptorData.java:186) ~[classes/:?]
    at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.prepareData(SwissRailRaptorFactory.java:77) ~[classes/:?]

In SwissRailRaptorData.java, lines 254-245 are the following:

        for (Map.Entry<TransitStopFacility, List<TransitStopFacility>> e : stopToStopsTransfers.entrySet()) {
            TransitStopFacility fromStop = e.getKey();
            Coord fromCoord = fromStop.getCoord();

So it looks as if fromStop is null, and the fromStop is coming from stopToStopTransfers.

Does you schedule use "minimal transfer times"? If so, please make sure that all listed transit stop ids are actually valid. My current guess is, that there is a minimalTransferTime for a stop that does not exist in the schedule.

vsgo222 commented 3 years ago

The log contains additional information, especially these lines:

Caused by: java.lang.NullPointerException
  at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.calculateRouteStopTransfers(SwissRailRaptorData.java:256) ~[classes/:?]
  at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorData.create(SwissRailRaptorData.java:186) ~[classes/:?]
  at ch.sbb.matsim.routing.pt.raptor.SwissRailRaptorFactory.prepareData(SwissRailRaptorFactory.java:77) ~[classes/:?]

In SwissRailRaptorData.java, lines 254-245 are the following:

        for (Map.Entry<TransitStopFacility, List<TransitStopFacility>> e : stopToStopsTransfers.entrySet()) {
            TransitStopFacility fromStop = e.getKey();
            Coord fromCoord = fromStop.getCoord();

So it looks as if fromStop is null, and the fromStop is coming from stopToStopTransfers.

Does you schedule use "minimal transfer times"? If so, please make sure that all listed transit stop ids are actually valid. My current guess is, that there is a minimalTransferTime for a stop that does not exist in the schedule.

You are absolutely right. It did had some missing transitStops. I modified my code to handle it. However, it now creates altogether a different issue. For e.g. ERROR BasicPlanAgentImpl:107 The agent 13121011900_2015000687560_853696_1 has destination link 863210156_F, but arrived on link 863210156_F_busonly. Setting agent state to ABORT Both link 863210156_F_busonly and link 863210156_F are exactly the same (first is a clone of a latter) and are overlaid one above other. the busonly link has only modes="bus,walk" in it while the original link has mode="bus,walk,car,tram". I tried different combination a) removing bus from the original link mode along "bus,walk" or b) removing "bus" from original link and only keeping "bus" in the busonly link. None worked. What should be the best approach to handle this scenario? Changing agents plans file to search and replace these linkIds would be extremely cumbersome (we have ~1.8 million agents). output_HighPTRdrshp_BusLane_v8.zip 13121011900_2015000687560_853696_1.zip

mrieser commented 3 years ago

How do you generate your plans? Usually, if one only provides x/y coordinate values, MATSim will automatically assign the nearest link and calculate routes so that everything matches.

If you get this error message The agent xyz has destination link abc, but arrived on link def then it usually means that the input (e.g. plans, network, schedule) don't match. So you either have to fix the input plans (yes, replacing is one way to do, but I wouldn't do it from hand but with a short Java code), or remove the route-information completely from the plans file (again, don't do it by hand for 1.8m agents) and let MATSim recalculate the necessary routes.

vsgo222 commented 3 years ago

How do you generate your plans? Usually, if one only provides x/y coordinate values, MATSim will automatically assign the nearest link and calculate routes so that everything matches.

If you get this error message The agent xyz has destination link abc, but arrived on link def then it usually means that the input (e.g. plans, network, schedule) don't match. So you either have to fix the input plans (yes, replacing is one way to do, but I wouldn't do it from hand but with a short Java code), or remove the route-information completely from the plans file (again, don't do it by hand for 1.8m agents) and let MATSim recalculate the necessary routes.

Dr. Rieser, The data was received as a input to us from the citycast service. Do you have any code which I can refer so that I can tailor it for the current usage? I think the "pt interaction" leg where start and end destination are been specifically mentioned is triggering such aborts

mrieser commented 3 years ago

Below is some example code to modify the link of activities. The code also deletes the routes before and after changed activities to force the routes to be newly calculated. I'm not sure if this is really necessary in your case, but I thought it's better to include it. I didn't run the code, but I think it should work this way... (*fingers crossed*)

Id<Link> oldLinkId = Id.create("123", Link.class);
Id<Link> newLinkId = Id.create("abc", Link.class);

Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
new PopulationReader(scenario).readFile("/path/to/population.xml.gz");

for (Person person : scenario.getPopulation().getPersons().values()) {
    for (Plan plan : person.getPlans()) {

        Leg prevLeg = null;
        boolean lastActivityChanged = false;

        for (PlanElement pe : plan.getPlanElements()) {
            if (pe instanceof Activity) {
                Activity act = (Activity) pe;
                if (oldLinkId.equals(act.getLinkId())) {
                    act.setLinkId(newLinkId);
                    if (prevLeg != null) {
                        prevLeg.setRoute(null); // force re-calculation of route
                    }
                    lastActivityChanged = true;
                }
            }
            if (pe instanceof Leg) {
                Leg leg = (Leg) pe;
                if (lastActivityChanged) {
                    leg.setRoute(null); // force re-calculation of route
                    lastActivityChanged = false;
                }
                prevLeg = leg;
            }
        }
    }
}

new PopulationWriter(scenario.getPopulation()).write("/path/to/newPopulation.xml.gz");