How do I read this error message?

Often when programming you will come across errors, and knowing how to interpret them is an important skill to have, as most of the time you spend programming will be spent fixing unexpected errors when they show up.

In this post I will be using Java exceptions, but the format and principle is the same for most programming languages.

Here is what a typical error may look like:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:659)
	at java.util.ArrayList.get(ArrayList.java:435)
	at Main.main(Main.java:8)

There are three main parts to this error:

  • The error type : java.lang.IndexOutOfBoundsException
  • The stack trace:
at java.util.ArrayList.rangeCheck(ArrayList.java:659)
at java.util.ArrayList.get(ArrayList.java:435)
at Main.main(Main.java:8)

Note that here we also get told what thread the error occurred in (Exception in thread "main"), but not all programming languages have this.

  • Additional info: Index: 0, Size: 0

Each of these gives us a different piece of information about the error.

The error type tells us what type of error it is; in this case it is a java.lang.IndexOutOfBoundsException, which is caused by trying to access a value outside of the range of an array or List.
You can find out what an error means by googling the error + the language. In this case I searched for IndexOutOfBoundsException java

The stack trace tells us where the error occurred. In this case, it occurred on line 659 in the file ArrayList.java, in the method ArrayList.rangeCheck.

Not very helpful, really, because we want to know where in our code the exception is happening. Luckily, by reading the stack trace a bit more, we can see that the we triggered the error on line 8 of the file Main.java, in the method Main.main.

Finally, we can get some additional info on why the exception occurred. Here, the Index: 0 part is telling us we tried to access the first element of the list (remember, you count from 0 in programming languages), while the list only had Size: 0, meaning it was empty.

So, we now know that the error is being caused because we are trying to access the first element of an empty list, on line 8 of the file Main.java.

And looking at the code, that’s exactly what’s happening!

import java.util.ArrayList;

public class Main {

	public static void main(String[] args) {
		
		ArrayList<String> test = new ArrayList<String>();
		String firstElement = test.get(0);
		
	}

}

The error is occurring because we are trying to access the first element of test (test.get(0)), but test is empty because we haven’t added anything to it yet.

Here are a few more examples of error messages and their meanings :

In Javascript :

Uncaught ReferenceError: test is not defined
    at main.js:1

Error type : ReferenceError
Stack trace : at main.js:1
Additional info : test is not defined
Interpretation : On line 1 of the file main.js, we are trying to use the variable test without it being defined.

In python :

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    for letter in test:
TypeError: 'int' object is not iterable

Error type : TypeError
Stack trace : File "main.py", line 2, in <module>. Python also gives us a preview of what line cause the error : for letter in test:
Additional info : 'int' object is not iterable
Interpretation : On line 2 of the file main.py, we are trying to iterate over a non-iterable type, int. (in this case, the variable test is an int and is defined as test = 57)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.