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

问题描述 投票:0回答:1

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

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

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

使用

traceln(chosen_users.getIndex())
打印索引,一切正常。当我将索引插入函数 get() 时,它返回错误。

即使我只插入一个随机数,比如说

users.get(1).receive("message")
,它仍然返回相同的错误(我的群体有 700 个代理)。

有什么想法吗?

anylogic
1个回答
1
投票

这只是因为 no 用户在某个特定时间处于

Inactive
状态,所以你的
randomWhere
调用返回
null

您也不需要使用索引的间接方式;只需使用

send
功能直接向其发送消息即可:

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

(您的命名也具有误导性,因为

chosen_users
始终是一个
User
代理(或
null
)。)

© www.soinside.com 2019 - 2024. All rights reserved.