Everybody makes mistakes, as it is part of the learning process. However, if you’re having a hard time understanding how and where to fix errors in AnyLogic, we’re here to help. This blog post can be used as a beginner’s guide that you refer to when errors appear.
The blog post is based on a recent webinar and focuses on the recommended strategies using AnyLogic's comprehensive out-of-the-box model building tools and paradigms that are available to all users.
As such, AnyLogic’s Debug mode – which allows you to suspend and resume the program, inspect variables, and evaluate expressions – is not covered. It is only available in AnyLogic Professional and is primarily useful for code-heavy models.
Here’s what this blog post covers:
- Model navigation
- Compile-time errors
- Cannot be resolved
- The method is not applicable
- Type mismatch
- Syntax error
- Unreachable code
- The method is undefined
- Statechart specific errors
- Runtime errors
Model navigation
If you’re new to AnyLogic, there are a few essential elements in the interface that you should be familiar with before you move any further.
At the top, you have a Menu and a Toolbar. From left to right, there are four sections:
- Projects view – where all your open models are located. As a beginner, you might want to have one model open at a time to avoid confusion.
- Palette view – contains AnyLogic library elements, presentation elements, and other tools for model building.
- Graphical editor or Canvas – a space where you drag and drop elements from the Palette to create both the model’s logic and the physical representation of your model.
- Properties view – where you see the properties of the model’s selected elements.
In the Projects section, the model’s elements are sorted out into different categories, so you can easily navigate through them. To have an agent open in the Graphical editor, double-click it. It will appear there as a new tab and will be highlighted in bold in the Projects view, so that you always know where you are in the model.
At the bottom, you’ll see a Status bar and two windows – Console and Problems. These two elements will be the focus of this blog post because they help you work with error messages.
If you're looking to advance in consulting and effectively integrate simulation modeling into your projects, download our free white paper.
Searching for model elements
Let’s say you want to find not an agent but a specific element that belongs to that agent on Canvas. When you first double-click the element, you will have the corresponding agent open. On the second double-click, the searched element in that agent will be highlighted.
Another way to search for elements is through the Search button (Ctrl + F for Windows or Command + F on a Mac).
These are the ways to find elements in your model that you will need when working with errors and these problematic elements.
Compile-time errors
These are found during code generation and compilation and appear before you’ve run the model. Such errors are displayed in the Problems view.
As you go along the model-building process, you can always press Build model (or F7) to find errors in all the models you have in Projects. If there are some, a small red X will appear near the model’s icon.
Open the Problems section at the bottom of your screen in AnyLogic to see all the errors that were detected. To filter them, use either Filter problems by model (click on it, then on a model in Projects) or Filter problems by selection (click on it, then on an agent in Projects that you want to check for errors).
Let’s look at the compilation problems that appeared in the webinar’s example models.
Compile-time error 1: "[NAME] cannot be resolved… (…to a variable/…or is not a field)"
This message could mean either:
- Something is wrong with the name you refer to, which is why AnyLogic can’t process it. It is usually caused by misspelling or renaming an object without refactoring.
- You didn’t specify the object type.
Here are some scenarios for dealing with this error.
Name resolution: the name of the referred element is wrong
The error indicates there’s a problem with the name of the population that is referred to. The modeler’s intention was that the Source block should refer to the agent population called Customers. So, here you can just delete mypopulation and, from the drop-down menu, select the suggested population name – Customers.
Now if you press Build model (or F7) again, you’ll see that you’ve resolved the error. Try this yourself with the Error demo – name resolution example [ZIP].
Name resolution: misspelling
Misspelling is one of the hardest things to catch. Sometimes, at first glance, there seems to be no typos, so there’s a trick that helps you make it right.
The modeler’s intention here was to add the time that the agents had spent going through the flowchart to the histogram. However, AnyLogic complains about the name in the code.
What you can do is leave the part of the name that you’re sure about, then call code completion (Ctrl + Space or Option + Space), and select the right option from the suggestions.
Now if you press Build model (or F7) again, you’ll see that you’ve resolved the error. Try this yourself with the Error demo – name resolution example [ZIP].
Name resolution: it doesn’t show up in code completion
You follow an error, and it indicates a mistake in naming, so you call code completion, but it doesn’t give you the right option. If the element you need is visible on Canvas but doesn’t show in code completion, check that it is not marked as Ignore.
Uncheck the Ignore option and press Build model (or F7) to see if the error is resolved. Try this yourself with the Error demo – name resolution example [ZIP].
Name field resolution: name of the parameter is wrong
The error indicates that the name of the Task priority: priority_value is incorrect. As this refers to an agent, and that agent is Customer, you should look for the solution in the elements of the Customer agent.
It turns out that there’s a parameter called priority_level, and that’s what you were looking for. Change the name in the Task priority parameter, and press Build model (or F7) to see if the error is gone. Try this yourself with the Error demo – name field resolution example [ZIP].
Name field resolution: object type is not specified
Unit points out a resource unit called employeePool. If you look for an element called skill_rating in the Employee agent, you will see it there, so the problem is not the naming as it was in the previous examples.
In the Seize block, where the error was found, the type of unit (agent) you refer to should be specified. First, you need to cast a specific unit: (Employee)unit. Then you need to let AnyLogic know that skill_rating belongs to the entire object ((Employee)unit).skill_rating.
Try this yourself with the Error demo – name field resolution example [ZIP].
Compile-time error 2: "The method [FUNCTION(ARG)] in the type [OBJECT_TYPE] is not applicable for the arguments [(ARG)]"
Such an error happens when an argument that you’re passing to a method or a function doesn’t match what it was expected to be.
What a user did was set the argument type to double, but then in the Delay block, they passed only one value. So, adding the second value resolves the issue.
Try this yourself with the Error demo – name field resolution example [ZIP].
Compile-time error 3: "Type mismatch: cannot convert from [TYPE_A] to [TYPE_B]"
You get this error if you have a variable of an incorrect type in the model.
In the example model, the problem is in the Condition of the SelectOutput block. Something is wrong with an element in the Customer agent. What you will find there is that the Type is set to Double while the Condition field of the SelectOutput block requires it to be Boolean. You change one to another, and the problem is solved.
Try this yourself with the Error demo – type mismatch example [ZIP].
Compile-time error 4: "Syntax error, [MESSAGE]"
These messages are quite straightforward. Let’s see what the most common messages are and how you can resolve them.
- Syntax error on token ")", delete this token. It means that there’s an extra character, which you need to remove.
- Syntax error, insert ")" to complete Expression. You have a character in an expression missing. An expression is a combination of variables, operators, and functions that evaluate to become one variable. Example: 2 * (3 + 4) would be an expression in the statement int result = 2 * (3 + 4);
- Syntax error, insert ";" to complete Statement. You have a character in a statement missing. A statement is a complete unit of execution in Java. It can perform an action or change a state. Example: int age = 25;
- Syntax error, insert "}" to complete BlockStatements. You have a character in a block statement that is missing. A block statement is a group of statements within curly braces {}.
Compile-time error 5: "Unreachable code"
If you see this error, you have code after terminating syntax, for example, after a semicolon ";".
Compile-time error 6: "The method [FUNCTION_NAME] is undefined for the type [OBJECT_TYPE]"
This error is quite straightforward too. It could happen because you’re calling a function that doesn’t exist for a certain object type. It is usually caused by a typo.
Compile-time error 7: compilation errors that are specific to statecharts
There are certain logical rules that must be followed in statecharts. The most common errors that you can see in this regard could be:
- Hanging statechart entry. The entry point isn’t connected to anything, but if you add a state from the Palette, the error will go away.
- Branch state without outgoing transitions. You simply need to add a new transition pointing from the Branch to another element.
Runtime errors
These happen during the model execution and are Java-related or logical. Such errors are detailed in the Console.
Runtime error 1: "Agent cannot leave port [NAME], it has no connections"
This simulation logic error is related to discrete-event flowcharts.
In this case, you would get the message "Agent cannot leave port root.shortDelay.out", so you need to add a Delay or a Sink block. Try this yourself with the Error demo – no port connections example [ZIP].
Runtime error 2: "An agent was not able to leave the port [NAME] at time [TIMESTAMP]/ date [DATESTAMP]"
This is another simulation logic and flowchart-related error. It is usually caused by the Source block when the capacity of the following block is reached.
What you can do is increase the capacity manually or use a Pull protocol. Try this yourself with the Error demo – leave port example [ZIP].
Runtime error 3: "[TYPE_A] cannot be cast to [TYPE_B]"
A Java code error that is triggered when you’re trying to cast an agent to an agent type that is not compatible with its actual runtime type.
Let’s say you have two agent types in a model: EmployeePrimary and EmployeeBackup. For those, you have two resource pools in Main.
But when you run the model, it tries to convert backup employees into primary ones, which causes an error. The solution here is to make the model check the actual class of the unit. Add the condition to the Unit rating parameter in the Seize block.
Try this yourself in the Error demo – incorrect casting example [ZIP].
Runtime error 4: "NullPointerException"
It’s triggered when you try to perform an operation directly on a null value ("null" is a Java keyword that indicates a lack of value).
See the Error demo – null pointer exception example [ZIP].
Runtime error 5: "IndexOutOfBoundsException"
It happens when you try to access a collection (e.g., an array or a list) at an invalid index based on the size of that collection. The correct index shouldn’t be below 0 or above the size of the collection.
You can play around with an index in the Error demo – index out of bounds example [ZIP].
Conclusion
Here we’ve covered some of the most common compile-time and runtime errors that you might see when you start building models in AnyLogic. Armed with the advice highlighted in this blog post, you can try dealing with more complex problems yourself. You can also search for answers and tips in AnyLogic Help: Troubleshooting.
And remember that you can always get support from the AnyLogic community on StackOverflow.
Let's move on from reading about common issues in simulation modeling to incorporating new knowledge into your projects.