Is it possible to make a static reference to an excel sheet in AnyLogic?

144 Views Asked by At

I want to call an Excel with a static java function within an AnyLogic Model as shown in the picture. The function "readExcelFile" has to be static, because I want to call it from another class within my AnyLogic Project. However, when I call the function, an error occurs: "Cannot make a static reference to the non-static field produktionssystem". Is there a solution for this problem?

enter image description here

2

There are 2 best solutions below

0
Stuart Rossiter On BEST ANSWER

The error is because the Excel element produktionssystem can only ever be a (non-static) element in whatever agent type your function is also in (Main from the screenshot). By definition a static function cannot see any elements (Java fields) inside agent instances.

But you should never need a static function unless you need to share it across runs (which can have dangerous side effects if you're not careful). Your agents that need to call it just need a reference to an agent instance that does have the function.

If the calling agent is a 'child' of the function-declaring agent, you've already got the "Link to upper level agent" main provided by AnyLogic (and this will always exist if the calling agent is anywhere in the model's hierarchy of agents). Otherwise your calling agent will need something like a parameter of type Main which is set by whoever creates it.

1
Ryan Marizza On

It's tough to understand what is causing the error here, because I can't see inside your function.

There is no inherent issue with read/write to Excel files from within another class (i.e. within another agent, custom class, etc...).

Here is a link to a public model on the AnyLogic cloud that I found very helpful when I first started working with Excel files within AnyLogic: https://cloud.anylogic.com/model/99d9f196-17e7-47c3-9b28-63a6f23b0dad?mode=SETTINGS