尝试从 AnyLogic 中的 Main 访问代理时出现 NullPointerException

NullPointerException when trying to access an agent from Main in AnyLogic

我正在尝试从 Main 向处于特定状态的代理发送消息。我已经这样做了很多次,但这次 AnyLogic 返回 NullPointerException 错误。

这是我用来发送消息的代码:

User chosen_user = randomWhere(users, t -> t.inState(User.Innactive));
users.get(chosen_user.getIndex()).receive("message");

使用traceln(chosen_users.getIndex())打印索引,一切正常。就在我将索引插入函数 get() 时,它 returns 错误。

即使我只插入一个随机数,比方说 users.get(1).receive("message"),它仍然 returns 同样的错误(我的人口有 700 个代理人)。

有什么想法吗?

这只是因为 没有 用户在某个特定时间处于 Innactive 状态,所以您的 randomWhere 调用 returns null.

你也不需要间接使用索引;只需使用 send 函数直接将消息发送给它:

if (chosen_users != null) {
   send("message", chosen_users);
}

(您的命名也具有误导性,因为 chosen_users 始终是一个 User 代理(或 null)。)