1

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

Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
DynamiX
  • 63
  • 4
  • What is your text inside that function? – Yashar Ahmadov Feb 09 '22 at 17:20
  • Can you add the reason why you want this to be static.. more often than not it is not needed in simulation models and there are ways around it... like e.g. putting the Excel sheet on Main and referencing it from there. – Jaco-Ben Vosloo Feb 09 '22 at 19:26
  • I put the Excel sheet on Main. Within my class I want to call the function "readExcelFIle". This function just calls the Excel File "produktionssystem" in Main and returns a double[] array. When I want to call the function "readExcelFile" via main.readExcelFile(...) it says "main cannot be resolved". Therefore I thougt I could call it via Main.readExcelFIle(..), but then the method has to be static and the ExcelFile produktionssystem as well. I do not want to initialize a new agent in my class, but I want to call the actual main agent. Where is my mistake? – DynamiX Feb 10 '22 at 08:35

2 Answers2

0

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

Ryan Marizza
  • 295
  • 2
  • 9
  • The reason for the error is in the screenshot; we don't need to know the code inside the function. (The error is already telling us it's referring to `produktionssytem`.) – Stuart Rossiter Feb 09 '22 at 22:24
0

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.

Stuart Rossiter
  • 2,432
  • 1
  • 17
  • 20