Search code examples
javasimulationanylogicagent

Getting NullPointerExection error while creating a text file for an Agent in AnyLogic


I have created an agent and then I put a text file from the Connectivity pallet into that Agent model. As soon as I run the simulation it is displaying NullPointerException error. In the console it says Unknown Source. Although I have created that file prior to this and put the location of that file from the properties pallet. I am using AnyLogic 7 pro.

  1. Error window

  2. log :

    Error during model startup: NullPointerException java.lang.NullPointerException at com.xj.anylogic.engine.Agent.getPresentation(Unknown Source) at com.xj.anylogic.engine.connectivity.TextFile.(Unknown Source) at com.xj.anylogic.engine.connectivity.TextFile.(Unknown Source) at warehouse.Worker.(Worker.java:71) at warehouse.Main._unloadingPerson_newUnit_xjal(Main.java:5246) at warehouse.Main.access$6(Main.java:5244) at warehouse.Main$7.newUnit(Main.java:1773) at com.xj.anylogic.libraries.processmodeling.ResourcePool.a(Unknown Source) at com.xj.anylogic.libraries.processmodeling.ResourcePool$b.a(Unknown Source) at com.xj.anylogic.libraries.processmodeling.ResourcePool.X(Unknown Source) at com.xj.anylogic.libraries.processmodeling.ResourcePool.onStartup(Unknown Source) at com.xj.anylogic.libraries.processmodeling.ResourcePool.start(Unknown Source) at warehouse.Main.start(Main.java:12747) at com.xj.anylogic.engine.Engine.start(Unknown Source) at com.xj.anylogic.engine.ExperimentSimulation.b(Unknown Source) at com.xj.anylogic.engine.ExperimentSimulation.run(Unknown Source) at warehouse.Simulation.executeShapeControlAction(Simulation.java:127)

  3. Properties window of the text file

I don't know what I am doing wrong. Note : This error doesn't happen when I put the same text file in the Main Agent model.

Update : I found a workaround. I linked the agent with the Main agent and then put the file in the Main agent. After that I called that file from the agent function. To my understanding I think except the main agent other agents can't create a connectivity object. I am not sure though.


Solution

  • Since you indicate that it is working when the same text file is placed on main, it might be the case that the agent gets created before the agent has access to the file...

    Here are a few options and checks

    Check 1: Ensure the file is accessible in the Resource section of the project under the data folder. It should not be greyed out and have a green dot on the left.

    enter image description here

    Check 2: There should also be a green dot next to the filename in the file object inside the agent where you placed it.

    enter image description here

    Alternative 1: Use a parameter inside the agent to pass the text file that exists on main to the agent to use it

    enter image description here

    enter image description here

    Alternative 2: If your agent lives on main you can access the text file on main directly from the agent.

    enter image description here

    Note: I don't think it is wise to have every agent access exactly the same text file from within themselves. It is better that they all access the same file object