diff --git a/toml_sort/tomlsort.py b/toml_sort/tomlsort.py
index f17d54e..e43d848 100644
--- a/toml_sort/tomlsort.py
+++ b/toml_sort/tomlsort.py
@@ -280,7 +280,9 @@ class TomlSort:
                 new_array_value.extend(comments)
             new_array_value.append(array_item)
 
-        if not (multiline and self.format_config.trailing_comma_inline_array):
+        if len(new_array_value) != 0 and not (
+            multiline and self.format_config.trailing_comma_inline_array
+        ):
             new_array_value[-1].comma = Whitespace("")
 
         if multiline:

diff --git a/tests/examples/inline.toml b/tests/examples/inline.toml
index 49a1718..f54ef3d 100644
--- a/tests/examples/inline.toml
+++ b/tests/examples/inline.toml
@@ -1,4 +1,6 @@
 [b]
+z = []
+q = {}
 blist=[    'c',     'a', 'b'] # Comment
 alist = [
   'g',
diff --git a/tests/examples/sorted/inline-default.toml b/tests/examples/sorted/inline-default.toml
index b47b062..71e586d 100644
--- a/tests/examples/sorted/inline-default.toml
+++ b/tests/examples/sorted/inline-default.toml
@@ -13,6 +13,8 @@ test = [
 ]
 
 [b]
+z = []
+q = {}
 blist = ['c', 'a', 'b'] # Comment
 alist = [
   'g',
diff --git a/tests/examples/sorted/inline-no-comments-no-table-sort.toml b/tests/examples/sorted/inline-no-comments-no-table-sort.toml
index ad7230f..bdfc385 100644
--- a/tests/examples/sorted/inline-no-comments-no-table-sort.toml
+++ b/tests/examples/sorted/inline-no-comments-no-table-sort.toml
@@ -17,6 +17,8 @@ alist = [
   'w',
 ]
 blist = ['a', 'b', 'c']
+q = {}
+z = []
 
 [a]
 test = [
diff --git a/tests/examples/sorted/inline.toml b/tests/examples/sorted/inline.toml
index e876ac7..b0985d4 100644
--- a/tests/examples/sorted/inline.toml
+++ b/tests/examples/sorted/inline.toml
@@ -37,3 +37,5 @@ alist = [
   'w'  # Comment inline
 ]
 blist = ['a', 'b', 'c']  # Comment
+q = {}
+z = []

IndexError exception on an empty array
TOML file:
```toml
# foo.toml

[tool.foo]
bar = []
```

Exception:
```
$ python -V
Python 3.11.0

$ toml-sort --version
0.22.0

$ toml-sort foo.toml
Traceback (most recent call last):
  File "/private/tmp/foo/.venv/bin/toml-sort", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/cli.py", line 356, in cli
    ).sorted()
      ^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 634, in sorted
    sorted_toml = self.toml_doc_sorted(toml_doc)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 620, in toml_doc_sorted
    item.key, self.toml_elements_sorted(item, sorted_document)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 439, in toml_elements_sorted
    item.key, self.toml_elements_sorted(item, previous_item)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 435, in toml_elements_sorted
    for item in self.sorted_children_table(original.children):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 387, in sorted_children_table
    non_tables = self.sort_items(
                 ^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 359, in sort_items
    item.value = self.sort_item(item.value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 306, in sort_item
    return self.sort_array(item, indent_depth=indent_depth)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 284, in sort_array
    new_array_value[-1].comma = Whitespace("")
    ~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

```
tests/test_cli.py::test_cli_defaults[inline-sorted/inline-default] tests/test_cli.py::test_cli_args[inline-path_sorted3-args3] tests/test_cli.py::test_cli_args[inline-path_sorted4-args4] tests/test_cli.py::test_cli_args[inline-path_sorted5-args5] tests/test_toml_sort.py::test_tomlsort[inline-inline-args0]
tests/test_cli.py::test_cli_defaults[from-toml-lang-sorted/from-toml-lang] tests/test_cli.py::test_cli_defaults[pyproject-weird-order-sorted/pyproject-weird-order] tests/test_cli.py::test_cli_defaults[comment-sorted/comment-header-footer] tests/test_cli.py::test_cli_args[comment-path_sorted0-args0] tests/test_cli.py::test_cli_args[comment-path_sorted1-args1] tests/test_cli.py::test_cli_args[comment-path_sorted2-args2] tests/test_cli.py::test_multiple_files_check[multiple tests/test_cli.py::test_multiple_files_check[single tests/test_cli.py::test_multiple_files_check[none tests/test_cli.py::test_multiple_files_in_place tests/test_cli.py::test_multiple_files_and_errors[--check tests/test_cli.py::test_multiple_files_and_errors[cannot tests/test_cli.py::test_load_config_file_read tests/test_cli.py::test_load_config_file[-expected0] tests/test_cli.py::test_load_config_file[[tool.other]\nfoo=2-expected1] tests/test_cli.py::test_load_config_file[[tool.tomlsort]-expected2] tests/test_cli.py::test_load_config_file[[tool.tomlsort]\nall=true-expected3] tests/test_cli.py::test_load_config_file[[tool.tomlsort]\nspaces_before_inline_comment=4-expected4] tests/test_cli.py::test_load_config_file_invalid[[tool.tomlsort]\nunknown=2] tests/test_cli.py::test_load_config_file_invalid[[tool.tomlsort]\nall=42] tests/test_toml_sort.py::test_sort_toml_is_str tests/test_toml_sort.py::test_tomlsort[comment-comment-comments-preserved-args1] tests/test_toml_sort.py::test_tomlsort[comment-comment-no-comments-args2] tests/test_toml_sort.py::test_tomlsort[comment-comment-header-footer-args3] tests/test_toml_sort.py::test_tomlsort[from-toml-lang-from-toml-lang-args4] tests/test_toml_sort.py::test_tomlsort[pyproject-weird-order-pyproject-weird-order-args5]