Error when running Hotelling's Law example
Describe the bug
In Hotelling Law example, running app.py gives the following error:
ERROR: Component react.component(__main__.SpaceDrawer) raised exception TypeError("'NoneType' object is not subscriptable")
Traceback (most recent call last):
File "/data/mesa/venv/lib/python3.12/site-packages/reacton/core.py", line 1702, in _render
root_element = el.component.f(*el.args, **el.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/mesa/mesa-examples/examples/hotelling_law/app.py", line 141, in SpaceDrawer
rect_x = pos[0] + (i * width)
~~~^^^
TypeError: 'NoneType' object is not subscriptable
Expected behavior According to the PNG file included with the example, agents should be positioned on a grid.
To Reproduce
$ cd mesa-examples/examples/hotelling_law/
$ git branch
* main
$ solara run app.py
Additional context
$ python --version
Python 3.12.11
$ python -c "import mesa; print(mesa.__version__)"
3.2.0
$ uname -a
Linux --- 5.15.0-139-generic #149-Ubuntu SMP Fri Apr 11 22:06:13 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Thanks @venzen, we will have it look into it, more than likely you can fix by installing Mesa < 3.2 (i.e. 3.15). But we will give it a look.
I’ve already read the contribution guidelines. Is this issue available, or can I proceed with it?
Go ahead!
Note that that is this example: https://github.com/projectmesa/mesa-examples/tree/main/examples/hotelling_law
I would recommend reproducing the issue first.
@EwoutH Is this issue available? Can I look on to it.
I think so, can you try reproducing it?
@EwoutH i have made a pull request for this , could you kindly check it
Please, as indicated also in a PR, we first need to have an example producing the bug so we can properly diagnose what is going on. Just adding some if statements to avoid the crash seems like patching it without addressing the potential root cause. Because in theory this bug should not be triggered at the solara side in the first place.
@quaquel alright understood! , first i tried to replicate the bug but it popped up right after running the application
The Hotelling Law visualization crashed because Solara renders the UI before all CellAgents have valid spatial data. I think in Mesa 3.x, agents store their position in agent.cell.coordinate, which can temporarily be None.
The visualization code seems to be assumed this coordinate always existed and tried to index into it, probably causing the TypeError.
I tryna fixed this by adding defensive checks to skip agents whose cell or coordinate is not yet available.
This seems to prevent render-time crashes without changing any model behavior.
Ok, good to know that the bug is still there. However, I still don't understand how this can happen. All the examples that ship with Mesa itself use Solara, and none of them show this behavior. The proper fix thus seems to update this example to use the modern mesa 3.3 way of creating a UI rather than trying to patch the currently dated example code.
totally valid , it would be better to fix this by implementing mesa 3.3 rather than the patch up