You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行集装箱运输仿真模型时遇'Trying to send message to null destination agent'错误

进口集装箱海港到干港运输仿真模型运行错误排查

问题背景

我正在开发进口集装箱从海港到干港运输的仿真模型,已创建以下Agent:Kandla_port、Dryport、Railyard、Train、Containers、Order。
模型核心流程:

  • Kandla港接收两类进口集装箱:twentyFootContainers(1 TEU)和fortyFootContainers(2 TEUs)
  • 两类集装箱通过两个独立事件按到达率在Kandla港生成
  • 当待发铁路的TEU数量超过列车最大运载量(90 TEU)的50%(即45 TEU)时,Kandla港需向Railyard发送列车请求
  • 已创建requestTrainIfNeeded函数,在每个集装箱到达后检查请求触发条件
  • 模型构建无错误,但运行时待发TEU数超过45时触发错误

运行错误信息

Exception during discrete event execution:
 root.kandla:
 Trying to send message to 'null' destination agent
 java.lang.RuntimeException: root.kandla:
 Trying to send message to 'null' destination agent
at com.anylogic.engine.Engine.error(Unknown Source)
at com.anylogic.engine.Agent.error(Unknown Source)
at com.anylogic.engine.Utilities.error(Unknown Source)
at com.anylogic.engine.AgentExtensionImpl.error(Unknown Source)
at com.anylogic.engine.ie.send(Unknown Source)
at com.anylogic.engine.Agent.send(Unknown Source)
at import_containers.Kandla.requestTrainIfNeeded(Kandla.java:749)
at import_containers.Kandla.executeActionOf(Kandla.java:726)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.n(Unknown Source)
at com.anylogic.engine.Engine.je(Unknown Source)
at com.anylogic.engine.Engine$i.run(Unknown Source)

相关代码

Event 1 - TwentyFootContainers

int containerId = containerIdCounter++; // Assign a unique ID to each container
Container container = new Container(true, weight, containerId); // Initialize a 20-foot container
twentyFootContainerCount++;
totalTeusArrived++;
if (Math.random() < roadModePercentage) {
    teusByRoad++;
} else {
    teusByRailPending++;
    requestTrainIfNeeded();  // Call the function to check if a train is needed
}

Event 2 - FortyFootContainers

int containerId = containerIdCounter++; // Assign a unique ID to each container
Container container = new Container(false, weight, containerId); // Initialize a 40-foot container
fortyFootContainerCount++;
totalTeusArrived += 2;
if (Math.random() < roadModePercentage) {
    teusByRoad += 2;
} else {
    teusByRailPending += 2;
    requestTrainIfNeeded();  // Call the function to check if a train is needed
}

Function - requestTrainIfNeeded

if (!trainRequested && teusByRailPending > capacity * 0.4) {
    Order order = new Order(this, teusByRailPending);
    send(order, railyard);
    trainRequested = true;
}

错误原因与解决方法

核心原因

错误提示Trying to send message to 'null' destination agent明确说明:Kandla港Agent中的railyard变量未正确初始化,指向了null,导致调用send(order, railyard)时无法找到目标Agent。

解决步骤

  1. 修正Railyard Agent的引用绑定

    • 在AnyLogic可视化界面中,打开Kandla_port Agent的属性面板,找到存储Railyard引用的railyard变量
    • 确保该变量已绑定到模型中实际存在的Railyard Agent实例,而非保持默认的null状态
    • 若使用参数传递或Agent连接逻辑,需在模型初始化阶段(如onStartup事件)完成railyard变量的赋值。
  2. 校准请求触发条件
    代码中当前触发条件为teusByRailPending > capacity * 0.4,但需求是超过45 TEU(90*50%),需修正为:

    teusByRailPending > capacity * 0.5
    

    或直接写teusByRailPending > 45,避免提前触发请求导致逻辑混乱。

  3. 添加空值防护(可选)
    在requestTrainIfNeeded函数中增加空值判断,避免因引用未初始化崩溃:

    if (!trainRequested && teusByRailPending > capacity * 0.5 && railyard != null) {
        Order order = new Order(this, teusByRailPending);
        send(order, railyard);
        trainRequested = true;
    } else if (railyard == null) {
        error("Railyard reference is null - cannot send train request");
    }
    

内容的提问来源于stack exchange,提问作者Nevil Gandhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:14:53