Search code examples
nullpointerexceptionexitsendanylogicagent

Why are parameters of an agent empty after creation and sending it to another agent?


In my Anylogic model I have agents (carriers) that receive orders and after some delay processes, should create a new truckOrder and send it to a truck agent. I have coded it in the exit block as you can see in the figure below. When running, I get a NullPointerException error within this exit block, because order.terminal and order.customer are empty.

enter image description here

This is how an order is created (within a customer agent) and send to the enter block of a carrier:

Order order = new Order(this, main.terminals(0));
Carrier carrier = randomFrom(main.carriers);
if (carrier != null)
    carrier.receiveOrder.take(order);

Also when I do it differently, like below, it does not work. Does anybody know why?

Order order = new Order(this, main.terminals(1));
Carrier carrier = randomFrom(main.carriers);
if (carrier != null)        
    send(order, carrier.receiveOrder);

The NullPointerException error is placed within these rows of the Carrier.java tab:

enter image description here


Solution

  • You have a spurious order Variable in Carrier.

    You are creating the type Order agent in Customer and passing it to the Enter block of Carrier. Thus (assuming you set the Enter block so that its "Agent type" is Order) you should be referring to it via the keyword agent in your block actions. (This keyword refers to the agent that is currently passing through the block, which is your Order agent.)

    Thus you should be using agent.terminal and agent.customer NOT order.terminal and order.customer (which are referring to your variable in Carrier which probably hasn't been set to anything).