【问题标题】:NullPointerException for List<T> variable in AnylogicAnylogic 中 List<T> 变量的 NullPointerException
【发布时间】:2022-08-20 17:35:22
【问题描述】:
我正在尝试为客户流失情况模拟同伴影响。代理将根据不同的条件向其对等方发送消息。我还创建了一个名为 MessagesReceived 的列表来存储收到的消息。(这些消息的数量可能大于 1)。
在 Agent\'s Connections>Communications>OnMessageReceived 中,我添加了this.Messages_Recieved.add(msg);,它应该将收到的消息添加到列表中。
现在我的代理的 onReceive 方法出现 NullPointerException 错误。
标签:
anylogic
agent-based-modeling
【解决方案1】:
系统工作正常并处理消息,但是当它尝试将新消息添加到列表时,它会遇到 NullPointerException,因为列表尚不存在。将 List 声明为变量是不够的。我们也需要启动它:
List<String> myList = new LinkedList<String>();
我已经能够在 Agent>Properties>Agent Actions>On StartUp 中启动这个变量:
Messages_Recieved = new LinkedList<String>();
另请注意,我们无法在 java 中使用 List<String> myList = new List<String>(); 启动 List
List 是通用的,您可以根据您使用的类创建不同的集合(有序/无序):
请参阅here 了解更多信息。我在这个例子中使用了 LinkedList。