🐛 Common Errors Guide
Every programmer makes mistakes — even experienced ones. The difference is knowing how to read error messages and fix them fast. This guide covers the errors you'll hit most often as a beginner, with clear explanations and fixes.
💡 How to Use This Page
When you see an error message you don't understand, use Ctrl+F (or Cmd+F on Mac) to search for the error name on this page. Each entry tells you what the error means, shows an example of broken code, and gives you the fix.
📑 Jump To
🐍 Python Errors
IndentationError: unexpected indent
What it means: Python uses indentation to define code blocks. Your indentation is inconsistent — either you mixed tabs and spaces, or a line is indented when it shouldn't be.
# ❌ Broken
def greet():
print("Hello")
print("World") # Extra indent!
# ✅ Fixed
def greet():
print("Hello")
print("World") # Same indent level
Fix: Make sure every line in the same block has the same indentation. Use 4 spaces (not tabs). In VS Code, turn on "Render Whitespace" to see invisible characters.
NameError: name 'x' is not defined
What it means: You're using a variable that doesn't exist yet — either you misspelled it, forgot to create it, or it's out of scope.
# ❌ Broken
print(username) # Never defined!
user_name = "Alice"
print(username) # Misspelled! (underscore)
# ✅ Fixed
user_name = "Alice"
print(user_name) # Matches exactly
Fix: Check the spelling carefully — Python is case-sensitive. Name and name are different variables.
TypeError: can only concatenate str (not "int") to str
What it means: You tried to combine a string and a number with +. Python won't auto-convert.
# ❌ Broken
age = 25
print("I am " + age + " years old")
# ✅ Fix 1: Convert to string
print("I am " + str(age) + " years old")
# ✅ Fix 2: Use f-string (better!)
print(f"I am {age} years old")
TypeError: 'str' object is not callable
What it means: You accidentally used a built-in function name as a variable, then tried to call it.
# ❌ Broken
list = [1, 2, 3] # Overwrote the built-in list()!
new_list = list("abc") # Now list is a variable, not a function
input = "hello" # Overwrote input()!
name = input("Name: ") # Crash!
# ✅ Fixed: Don't use built-in names as variables
my_list = [1, 2, 3]
user_input = "hello"
Fix: Never name variables list, dict, str, int, input, print, type, or id.
KeyError: 'name'
What it means: You tried to access a dictionary key that doesn't exist.
# ❌ Broken
user = {"email": "a@b.com"}
print(user["name"]) # Key doesn't exist!
# ✅ Fix 1: Use .get() with a default
print(user.get("name", "Unknown"))
# ✅ Fix 2: Check first
if "name" in user:
print(user["name"])
IndexError: list index out of range
What it means: You tried to access an index that doesn't exist in the list.
# ❌ Broken
items = ["a", "b", "c"]
print(items[3]) # Only indices 0, 1, 2 exist!
# ✅ Fixed: Check length first
if len(items) > 3:
print(items[3])
# Remember: last item is items[len(items) - 1] or items[-1]
ModuleNotFoundError: No module named 'requests'
What it means: You're importing a package that isn't installed.
# ❌ Error: import requests → ModuleNotFoundError
# ✅ Fix: Install it first
# pip install requests
# (or: pip install requests --break-system-packages)
Fix: Run pip install <package_name> in your terminal. Make sure you're using the same Python that your script runs with (python -m pip install requests is safest).
⚡ JavaScript Errors
ReferenceError: x is not defined
What it means: Same as Python's NameError — the variable doesn't exist.
// ❌ Broken
console.log(userName); // Never declared!
// ✅ Fixed
let userName = "Alice";
console.log(userName);
Common cause: Misspelling, or accessing a variable outside its let/const block scope.
TypeError: Cannot read properties of undefined (reading 'x')
What it means: You're trying to access a property on something that is undefined. This is the #1 JavaScript error.
// ❌ Broken
let user = undefined;
console.log(user.name); // Can't read .name on undefined!
let data = { user: null };
console.log(data.user.name); // Can't read .name on null!
// ✅ Fix 1: Optional chaining
console.log(data.user?.name); // undefined (no crash)
// ✅ Fix 2: Check first
if (data.user) {
console.log(data.user.name);
}
Fix: Use optional chaining (?.) or check that the object exists before accessing properties.
TypeError: x is not a function
What it means: You're trying to call something that isn't a function — often because you overwrote it or misspelled it.
// ❌ Broken
let greet = "hello";
greet(); // "hello" is a string, not a function!
// Also common: forgetting await on async functions
const data = fetch(url); // data is a Promise, not the response!
data.json(); // TypeError!
// ✅ Fixed
const response = await fetch(url); // await the Promise
const data = await response.json();
SyntaxError: Unexpected token
What it means: JavaScript found a character it didn't expect — usually a missing bracket, parenthesis, or comma.
// ❌ Missing closing brace
function greet() {
console.log("hi");
// } ← forgot this!
// ❌ Missing comma in object
const user = {
name: "Alice"
email: "a@b.com" // Missing comma after "Alice"!
};
// ✅ Fixed
const user = {
name: "Alice",
email: "a@b.com"
};
Fix: The error message tells you the line number. Look at that line and the line above for missing {, }, (, ), or ,.
== vs === (Silent Logic Bug)
What it means: Not an error message — but a constant source of bugs. == does type coercion; === does not.
// ❌ Surprising behavior with ==
"5" == 5 // true (string "5" coerced to number!)
0 == false // true
"" == false // true
null == undefined // true
// ✅ Always use ===
"5" === 5 // false (different types)
0 === false // false
"" === false // false
Rule: Always use === and !== in JavaScript. There's almost never a reason to use ==.
SyntaxError: await is only valid in async functions
What it means: You used await outside an async function.
// ❌ Broken
function fetchData() {
const response = await fetch(url); // Can't use await here!
}
// ✅ Fixed: Add the async keyword
async function fetchData() {
const response = await fetch(url); // Now it works
}
🔷 C# Errors
CS0029: Cannot implicitly convert type 'string' to 'int'
What it means: C# is strictly typed — you can't assign a string to an int variable without explicit conversion.
// ❌ Broken
int age = Console.ReadLine(); // ReadLine returns string!
// ✅ Fixed
int age = int.Parse(Console.ReadLine()!);
// ✅ Safer: TryParse
if (int.TryParse(Console.ReadLine(), out int age))
Console.WriteLine($"Age: {age}");
else
Console.WriteLine("That's not a number!");
NullReferenceException: Object reference not set to an instance of an object
What it means: You tried to use a variable that is null. C#'s equivalent of JavaScript's "Cannot read properties of undefined."
// ❌ Broken
string? name = null;
Console.WriteLine(name.Length); // Crash! name is null!
// ✅ Fix 1: Null check
if (name != null)
Console.WriteLine(name.Length);
// ✅ Fix 2: Null-conditional operator
Console.WriteLine(name?.Length ?? 0); // 0 if null
CS0103: The name 'x' does not exist in the current context
What it means: The variable is out of scope or was never declared.
// ❌ Broken: Variable declared inside if block
if (true)
{
string message = "Hello";
}
Console.WriteLine(message); // message doesn't exist here!
// ✅ Fixed: Declare in the outer scope
string message = "";
if (true)
{
message = "Hello";
}
Console.WriteLine(message);
FormatException: Input string was not in a correct format
What it means: You called int.Parse() or double.Parse() on a string that isn't a valid number.
// ❌ Crashes if user types "abc"
int num = int.Parse(Console.ReadLine()!);
// ✅ Fixed: Use TryParse
if (int.TryParse(Console.ReadLine(), out int num))
Console.WriteLine($"Number: {num}");
else
Console.WriteLine("Invalid number!");
CS1503: Cannot convert from 'string' to 'char'
What it means: C# distinguishes between string (double quotes) and char (single quotes). Some methods need a char.
// ❌ Broken
string[] parts = text.Split(","); // Split wants a char!
// ✅ Fixed: Use single quotes for char
string[] parts = text.Split(','); // char ','
// Or use the string overload explicitly
string[] parts = text.Split(",", StringSplitOptions.None);
CS8600/CS8602: Possible null reference
What it means: The compiler warns that a value might be null and you're using it without checking. This is a warning, not an error, but ignoring it leads to runtime crashes.
// ⚠️ Warning: ReadLine() might return null
string name = Console.ReadLine(); // CS8600
// ✅ Fix 1: Null-forgiving operator (you know it won't be null)
string name = Console.ReadLine()!;
// ✅ Fix 2: Handle the null case
string? name = Console.ReadLine();
if (!string.IsNullOrEmpty(name))
Console.WriteLine(name.ToUpper());
Universal Mistakes (All Languages)
Off-By-One Error
What it is: Accessing one past the end of a collection, or looping one too many/few times.
# All languages: indices start at 0, not 1!
items = ["a", "b", "c"] # indices: 0, 1, 2
# items[3] → ERROR (only 0-2 exist)
# Last item: items[len(items) - 1] or items[-1] (Python)
Rule: If a list has N items, valid indices are 0 through N-1.
Infinite Loop
What it is: A while loop whose condition never becomes false. Your program freezes.
# ❌ Infinite! count never changes
count = 0
while count < 10:
print(count)
# Forgot: count += 1
# ✅ Fixed
count = 0
while count < 10:
print(count)
count += 1 # This makes the loop eventually end
Fix: Make sure your loop condition will eventually become false. If stuck, press Ctrl+C to kill the program.
Modifying a List While Iterating
What it is: Removing items from a list while looping over it causes skipped items or crashes.
# ❌ Broken (Python) — skips items!
items = [1, 2, 3, 4, 5]
for item in items:
if item % 2 == 0:
items.remove(item) # Don't do this!
# ✅ Fixed: Create a new filtered list
items = [x for x in items if x % 2 != 0]
# Or iterate over a copy
for item in items[:]: # [:] makes a copy
if item % 2 == 0:
items.remove(item)
Assignment (=) vs Comparison (==)
What it is: Using = (assign) when you meant == (compare). In Python this is a SyntaxError in if statements. In JS/C# it can silently do the wrong thing.
// ❌ JavaScript: This ASSIGNS, doesn't compare!
if (x = 5) { // Always true! Sets x to 5
// ...
}
// ✅ Fixed
if (x === 5) { // Compares x to 5
// ...
}
How to Read Error Messages
Error messages look scary, but they follow a pattern. Here's how to decode them:
✅ The Three Things Every Error Message Tells You
- The error type — What kind of mistake (NameError, TypeError, SyntaxError)
- The message — A description of what went wrong
- The location — The file name and line number where it happened
Always start by reading the last line of the error (that's the actual error). Then look at the line number. The bug is usually on that line or the line above it.
# A Python traceback reads BOTTOM to TOP:
#
# Traceback (most recent call last): ← "here's what happened"
# File "app.py", line 15, in main ← called from main(), line 15
# result = process(data) ← which called process()
# File "app.py", line 8, in process ← process() is where it crashed
# return data["name"] ← this is the actual broken line
# KeyError: 'name' ← THIS is the error
#
# Read: "On line 8, inside process(), I tried to access
# the key 'name' but it doesn't exist in the dictionary."
// A JavaScript error reads TOP to BOTTOM:
//
// TypeError: Cannot read properties of undefined (reading 'name')
// at process (app.js:8:15) ← crashed here
// at main (app.js:15:20) ← called from here
// at Object.<anonymous> (app.js:20:1)
//
// Read: "On line 8 of app.js, inside process(), I tried
// to read .name on something that was undefined."
// A C# exception reads TOP to BOTTOM:
//
// Unhandled exception. System.NullReferenceException:
// Object reference not set to an instance of an object.
// at Program.Process(String data) in Program.cs:line 8
// at Program.Main() in Program.cs:line 15
//
// Read: "On line 8 of Program.cs, inside Process(),
// I tried to use a variable that was null."
💡 The Debugging Checklist
- Read the error message. It tells you what's wrong.
- Find the line number. Go to that line in your code.
- Check the line above too. The real problem is often there.
- Add a print statement before the broken line to see what the variables actually contain.
- Google the error message. Someone has had the exact same problem. Stack Overflow is your friend.