AP CSP: Error Cheat Sheet

Read the message, find the line, fix one thing, reload. Works for the Mad Libs project in Codio and for any JavaScript.

Click a triangle to open a section.

1. How to read an error message

[ERROR] Uncaught TypeError: Cannot read properties of null (reading 'value') at https://...codio.io/.../madlibs/code.js:12:49
  • Uncaught nothing in your code handled the problem, so the program stopped.
  • TypeError the kind of error. It tells you where to look (see the lookup cards below).
  • the message what went wrong, in plain words once you know the pieces.
  • code.js : 12 : 49 the file, the line number, and the column (how far across the line).
Fix the first error first. Later errors are often caused by the first one. Save, reload the Preview, and see what is left.
The line number is where JavaScript noticed the problem, not always where you made it. For a missing } the error points to the END of the file, because that is where JavaScript finally gave up waiting.

Where to see errors. In Codio, errors print in the Preview console, like the ones above. In Chrome you can also open DevTools: press F12, or Ctrl+Shift+J on Windows and Chromebook, or Cmd+Option+J on Mac, and click Console.

Why line numbers matter. The line number tells you exactly where to start looking, so you read ONE line instead of hunting through the whole file. Before you change anything, write down three things: the file, the line, and the column.

Try it: error decoder

Paste a whole error from the console (or just type the message). The decoder pulls out the kind of error, the message, and the file and line, and shows you the cards that match.

Or try an example:

2. Line detective: click the line the error points to

Reading the line number is a skill. Each case below shows a real error message and the real code. Click the line the error is talking about. Some cases have a second step, because the line the error names is not always where the mistake is.

3. ( ) vs { } vs [ ] (and quotes)

SymbolNameUsed forExampleThe mistake you will see
( )parenthesescalling a function, the inputs of a function, and the condition of an if or loopalert("Hi")
function go(name)
if (age > 12)
a missing ) gives missing ) after argument list. Leaving ( ) off an if or a function name gives Unexpected identifier or Unexpected token '{'
{ }curly bracesa block of code: what is inside a function, an if/else, or a loopfunction go() { ... }a missing } gives Unexpected end of input. An extra } gives Unexpected token '}'
[ ]square bracketsa list (array), and picking one item from itvar words = ["bat", "cat"];
words[0]
using ( ) to make a list gives no error and a wrong answer. The first item is index 0, so the last of 3 items is words[2]
" "quotestext (a string). ' ' works too, but never mix them on one piece of text"spooky"a missing closing quote gives Invalid or unexpected token. No quotes at all makes JavaScript look for a variable: bat is not defined
The one rule. Every opener needs a closer, in the right order. The last thing opened is the first thing closed:
function go() {          // opens {
  if (age > 12) {        // opens ( ... ) then {
    alert("Teen");       // opens ( ... ) with quotes
  }                      // closes the if's {
}                        // closes the function's {

Tips. Click right next to a bracket in Codio and the editor highlights its partner. Indent the code inside every { } the same amount, and the line where the indentation stops matching is usually where a } is missing.

Same symbols on the AP exam. The text version of the AP pseudocode uses { } for blocks, ( ) for conditions and procedure calls, and [ ] for list items, and list[1] is the first item there. In JavaScript the first item is list[0].

4. Error lookup: 21 common errors

Every message and fix here was run in a real browser. Click an error to open it. Pick a type to show only that kind, or press All. You can also search, or use the error decoder in section 1.

Cannot read properties of null (reading 'value')

Run-timeTypeError
Uncaught TypeError: Cannot read properties of null (reading 'value')

What it means. Your code asked the page for an element and got nothing (null), then tried to read .value from nothing.

Common causes

  • The id in your JavaScript is spelled differently than the id in your HTML (spelling and capital letters count).
  • The element has no id at all, or it was never added to the page.
  • The script runs before the page has finished loading (see the "when the page loads" error).

How to find it. Look at the line number in the error. Find every getElementById on that line. Put the id from the JavaScript next to the id in index.html and compare letter by letter.

Seen in Mad Libs: code.js line 12 read .value from an element that was not found.

Broken
function makeStory() {
  var adj = document.getElementById("adjectve").value;   // <-- id is misspelled
  document.getElementById("story").innerHTML = "A " + adj + " night.";
}
Fixed
function makeStory() {
  var adj = document.getElementById("adjective").value;   // <-- matches id="adjective"
  document.getElementById("story").innerHTML = "A " + adj + " night.";
}

Cannot set properties of null (setting 'innerHTML')

Run-timeTypeError
Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')

What it means. Almost the same problem as "Cannot read properties of null (reading 'value')", but this time you tried to put text INTO an element that was not found.

Common causes

  • The id in getElementById does not match any id in index.html.
  • A capital letter is different: story and Story are different ids.

How to find it. Find the getElementById on the line the error names, then search index.html for that exact id.

Broken
function makeStory() {
  var adj = document.getElementById("adjective").value;
  document.getElementById("storey").innerHTML = "A " + adj + " night.";   // <-- storey?
}
Fixed
function makeStory() {
  var adj = document.getElementById("adjective").value;
  document.getElementById("story").innerHTML = "A " + adj + " night.";   // <-- matches id="story"
}

Cannot read properties of null (reading 'value') (when the page loads)

Run-timeTypeError
Uncaught TypeError: Cannot read properties of null (reading 'value')

What it means. The script ran before the browser had created the input box, so the element did not exist yet. The ids are fine. The timing is wrong.

Common causes

  • A line outside any function reads from the page, and the <script> tag is above the HTML it needs.
  • The error shows up as soon as the page opens, before you click anything.

How to find it. Look for lines that use getElementById but are not inside a function. The fix is to read the box inside the function, so it runs when the button is clicked.

Broken
<!doctype html><html><head><meta charset="utf-8">
<script>
var adj = document.getElementById("adjective").value;   // <-- runs too early
function makeStory() {
  document.getElementById("story").innerHTML = "A " + adj + " night.";
}
</script></head><body>
<input id="adjective" value="spooky">
<p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>
Fixed
<!doctype html><html><head><meta charset="utf-8">
<script>
function makeStory() {
  var adj = document.getElementById("adjective").value;   // <-- read it when the button is clicked
  document.getElementById("story").innerHTML = "A " + adj + " night.";
}
</script></head><body>
<input id="adjective" value="spooky">
<p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>

Cannot read properties of undefined (reading 'length')

Run-timeTypeError
Uncaught TypeError: Cannot read properties of undefined (reading 'length')

What it means. You asked for a list item that does not exist, so you got undefined, and then tried to use it.

Common causes

  • Lists in JavaScript start at index 0, not 1. A list with 3 items has indexes 0, 1, and 2.
  • The index is bigger than the last item.

How to find it. Count the items in the list, remember the first is index 0, and compare to the number inside the [ ].

Broken
var words = ["bat", "ghost", "witch"];
var last = words[3].length;   // <-- there is no words[3]
Fixed
var words = ["bat", "ghost", "witch"];
var last = words[2].length;   // <-- the last item is index 2

count is not a function

Run-timeTypeError
Uncaught TypeError: count is not a function

What it means. You put ( ) after something that is not a function, so JavaScript tried to run it and could not.

Common causes

  • A variable and a function share the same name.
  • You added ( ) after a plain variable.

How to find it. Look at the name before the ( ) in the error. Is it a variable that holds a value, or a function you defined with function?

Broken
var count = 3;
var result = count();   // <-- count is a number, not a function
Fixed
var count = 3;
var result = count;   // <-- just use the value

document.getElementbyId is not a function

Run-timeTypeError
Uncaught TypeError: document.getElementbyId is not a function

What it means. JavaScript does not know a method with that exact spelling. Method names are case sensitive.

Common causes

  • getElementById has a capital B and a capital I: get Element By Id.
  • Check the capital letters in any built-in name, such as innerHTML, toUpperCase, and getElementById.

How to find it. Read the name in the error exactly. Compare it to the correct spelling. One small capital letter is enough to break it.

Broken
function makeStory() {
  var adj = document.getElementbyId("adjective").value;   // <-- lowercase b
  document.getElementById("story").innerHTML = adj;
}
Fixed
function makeStory() {
  var adj = document.getElementById("adjective").value;   // <-- capital B
  document.getElementById("story").innerHTML = adj;
}

nuon is not defined

Run-timeReferenceError
Uncaught ReferenceError: nuon is not defined

What it means. You used a name that JavaScript has never heard of. It does not exist in your program.

Common causes

  • A typo in a variable name: noun versus nuon.
  • You forgot the quotes around text, so JavaScript thought bat was a variable.
  • You used a variable outside the function where it was created.
  • You used it before the line that creates it.

How to find it. Take the name in the error. Search your code for where you created it with var. If you cannot find it spelled exactly the same, that is the bug.

Broken
var noun = "bat";
console.log(nuon);   // <-- misspelled
Fixed
var noun = "bat";
console.log(noun);   // <-- same spelling as the var

Unexpected end of input

SyntaxBracketsSyntaxError
Uncaught SyntaxError: Unexpected end of input

What it means. JavaScript reached the end of your file and was still waiting for a } or ) that never came. Something was opened and never closed.

Common causes

  • A missing } at the end of a function, if, or loop.
  • A missing ) closing a function call.

How to find it. Count your { and } . They must match. Look at the indentation: the line where the indentation stops matching is usually where the missing } belongs. The error line number is the END of the file, not where you made the mistake.

Seen in Mad Libs: code.js line 58, column 1 is the last line of the file, where JavaScript gave up waiting for a }.

Broken
function makeStory() {
  var adj = "spooky";
  if (adj === "spooky") {
    alert("Boo!");
  // <-- the if is closed? no: a } is missing here
}
Fixed
function makeStory() {
  var adj = "spooky";
  if (adj === "spooky") {
    alert("Boo!");
  }   // <-- closes the if
}

Unexpected token '}'

SyntaxBracketsSyntaxError
Uncaught SyntaxError: Unexpected token '}'

What it means. JavaScript found a } where it did not expect one. There is one closing brace too many, or an opening brace is missing earlier.

Common causes

  • An extra } after a function.
  • A { was deleted by accident.

How to find it. Count the { and } again. The line number points to the extra }, or to the first place the braces stop matching.

Broken
function sayBoo() {
  alert("Boo!");
}}   // <-- one } too many
Fixed
function sayBoo() {
  alert("Boo!");
}   // <-- one } closes one {

missing ) after argument list

SyntaxBracketsSyntaxError
Uncaught SyntaxError: missing ) after argument list

What it means. Inside the ( ) of a function call, JavaScript found something it did not expect, so it thinks the ) is missing.

Common causes

  • Two things joined together without a + sign: "Hi " name.
  • Two items without a comma between them.
  • A ) missing at the end of the call.

How to find it. Look inside the ( ) on that line. Between every piece of text and every variable there must be a + or a comma.

Broken
var name = "Sam";
alert("Hi " name);   // <-- missing +
Fixed
var name = "Sam";
alert("Hi " + name);   // <-- + joins the text and the variable

Invalid or unexpected token

SyntaxBracketsSyntaxError
Uncaught SyntaxError: Invalid or unexpected token

What it means. JavaScript found a character it cannot make sense of. The most common cause is a quote that was opened and never closed.

Common causes

  • A missing closing quote: "spooky.
  • Mixing a " with a ' on the same piece of text.
  • Curly (smart) quotes copied from a document.

How to find it. Look at the colors in your editor. Text inside quotes is usually one color. If the color keeps going to the end of the line, a quote is missing.

Broken
var word = "spooky;   // <-- no closing quote
alert(word);
Fixed
var word = "spooky";   // <-- closing quote added
alert(word);

Unexpected identifier 'adj'

SyntaxSyntaxError
Uncaught SyntaxError: Unexpected identifier 'adj'

What it means. Two names or values showed up next to each other with nothing between them, like a missing +, comma, or semicolon.

Common causes

  • A missing + between text and a variable.
  • A missing semicolon or line break.
  • A missing { or ( earlier.

How to find it. Look at the line number and the word the error names. What is directly before that word? Something should join them.

Broken
var adj = "spooky";
var story = "A " adj;   // <-- missing +
Fixed
var adj = "spooky";
var story = "A " + adj;   // <-- + added

Unexpected token '{'

BracketsSyntaxError
Uncaught SyntaxError: Unexpected token '{'

What it means. A function needs ( ) after its name, even when it takes nothing in. Without them, JavaScript does not expect a { yet.

Common causes

  • function makeStory { ... } instead of function makeStory() { ... }.

How to find it. Look at the function line the error points to. Does the name have ( ) right after it?

Broken
function makeStory {   // <-- no ( ) after the name
  alert("Boo!");
}
Fixed
function makeStory() {   // <-- ( ) added
  alert("Boo!");
}

Unexpected identifier 'age'

BracketsSyntaxError
Uncaught SyntaxError: Unexpected identifier 'age'

What it means. The condition of an if must be inside ( ). Without them JavaScript does not know where the condition starts.

Common causes

  • if age > 12 { ... } instead of if (age > 12) { ... }.

How to find it. Check every if and while line: the condition belongs inside ( ), and the code to run belongs inside { }.

Broken
var age = 14;
if age > 12 {   // <-- no ( ) around the condition
  alert("Teen");
}
Fixed
var age = 14;
if (age > 12) {   // <-- ( ) around the condition
  alert("Teen");
}

Unexpected token '('

BracketsSyntaxError
Uncaught SyntaxError: Unexpected token '('

What it means. You used ( ) where a { } block belongs. Parentheses hold a condition or a list of inputs. A block of code goes in curly braces.

Common causes

  • function sayBoo() ( ... ) instead of function sayBoo() { ... }.

How to find it. After the ( ) of a function, if, or loop, the next symbol must be {.

Broken
function sayBoo() (   // <-- should be {
  alert("Boo!");
)
Fixed
function sayBoo() {   // <-- curly braces hold the block
  alert("Boo!");
}

Failed to load resource: the server responded with a status of 404

File namesConsole message
Failed to load resource: the server responded with a status of 404 (Not Found)
then, when you click:  Uncaught ReferenceError: makeStory is not defined

What it means. The browser looked for a file named in your HTML and the file does not exist under that name. Nothing in that file ran, so your functions do not exist.

Common causes

  • <script src="script.js"> but the file is really named code.js.
  • Capital letters differ: Code.js and code.js are different files.
  • The file is in a different folder.
  • A space or an extra letter in the name.

How to find it. Open the file list. Read the real file name. Then read the src in index.html. They must be identical, including capitals and the extension.

Seen in Mad Libs: the file list says code.js but index.html loads script.js.

Broken
<!doctype html><html><head><meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>   <!-- <-- the file is really named code.js -->
</head><body>
<input id="adjective" value="spooky"><p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>
Fixed
<!doctype html><html><head><meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<script src="code.js"></script>   <!-- <-- matches the real file name -->
</head><body>
<input id="adjective" value="spooky"><p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>

makeStory is not defined (after a missing script)

File namesReferenceError
Uncaught ReferenceError: makeStory is not defined

What it means. This is what happens when you click the button and the script file never loaded. The function lives inside the file the browser could not find.

Common causes

  • The script file name in index.html does not match the real file name, so the browser never read your functions.

How to find it. If you see 'is not defined' for a function you know you wrote, check the Console for a 404 first. If the file did not load, fix the file name before you look at the code inside it.

Broken
<!doctype html><html><head><meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>   <!-- <-- the file is really named code.js -->
</head><body>
<input id="adjective" value="spooky"><p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>
Fixed
<!doctype html><html><head><meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<script src="code.js"></script>   <!-- <-- matches the real file name -->
</head><body>
<input id="adjective" value="spooky"><p id="story"></p>
<button id="go" onclick="makeStory()">Make my story</button>
</body></html>

404 on style.css or an image (the page looks wrong)

File namesConsole message
Failed to load resource: the server responded with a status of 404 (Not Found)

What it means. A missing style file means the page has no colors or layout. A missing image shows a broken picture icon. There may be no red error in your code at all, so check the Console for 404.

Common causes

  • <link href="styles.css"> but the file is style.css.
  • madlibs.jpg in the HTML but the file is madlibs.jpeg: the extension has to match too.

How to find it. Compare every file name in your HTML with the file list: style.css, the script, and each image.

Broken
<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="styles.css"></head><body>
<img src="madlibs.jpg" alt="cover">   <!-- <-- the file is madlibs.jpeg -->
<p>Hello</p></body></html>
Fixed
<!doctype html><html><head><meta charset="utf-8"><link rel="stylesheet" href="style.css"></head><body>
<img src="madlibs.jpeg" alt="cover">   <!-- <-- matches the real file -->
<p>Hello</p></body></html>

No error message, but the wrong thing happens

LogicLogic error
(no error message appears)

What it means. The program runs without crashing, but it gives a wrong answer. Computers do exactly what you wrote, not what you meant.

Common causes

  • One = stores a value. Two or three === compares. if (guess = 5) changes guess to 5 and is always true.
  • A condition that is backward: > instead of <.
  • The wrong variable used in a line.

How to find it. Use console.log to print the values the code is using, then compare them to what you expected. Read the line out loud as plain English.

Broken
var guess = 3;
var out;
if (guess = 5) {   // <-- = stores 5 into guess, it does not compare
  out = "You win!";
} else {
  out = "Try again";
}
window.__out = out;
Fixed
var guess = 3;
var out;
if (guess === 5) {   // <-- === compares
  out = "You win!";
} else {
  out = "Try again";
}
window.__out = out;

Numbers from a text box act like text (5 + 1 becomes 51)

LogicLogic error
(no error message appears)

What it means. Whatever is typed into an input box arrives as TEXT, even if it looks like a number. Adding text to a number joins them instead of adding.

Common causes

  • Reading .value from an input and adding to it.

How to find it. console.log the value and its type. If + joins things together instead of adding, turn the text into a number first with Number( ).

Broken
var age = document.getElementById("age").value;
window.__out = String(age + 1);   // <-- "5" + 1 is "51"
Fixed
var age = Number(document.getElementById("age").value);   // <-- convert to a number
window.__out = String(age + 1);

No error message, but the list is not a list

BracketsLogic error
(no error message appears)

What it means. Parentheses ( ) around a few values do not make a list. Lists use square brackets [ ]. JavaScript quietly takes the last value instead.

Common causes

  • var words = ("bat", "cat"); instead of [ ].

How to find it. If words[0] gives you one letter, or the whole list acts strange, check the brackets where the list is created.

Broken
var words = ("bat", "cat");   // <-- ( ) is not a list
window.__out = words[0];
Fixed
var words = ["bat", "cat"];   // <-- [ ] makes a list
window.__out = words[0];
Nothing matches that. Try fewer words, like the error type (TypeError) or one word from the message.

5. Find bugs with console.log and alert

Line numbers tell you where JavaScript crashed. A logic bug does not crash anything, so there is no line number to read. You find those by printing values and looking at them. Two tools do that:

console.log(x)alert(x)
Where it showsthe console, in the same place errors appeara popup box in the middle of the page
Stops your program?No. The program keeps runningYes. Nothing happens until you click OK
Best forseeing many values, loops, and what a value is at different momentsa quick "did this line run?" check
Watch outyou have to open the console to see itinside a loop you get one popup every time through
  1. Label it. console.log("i is", i); is easier to read than console.log(i);.
  2. Check before and after the line you think is wrong. If the value changed in between, that line did it.
  3. Check the type. console.log(typeof age); tells you whether you have text ("string") or a number.
  4. Did this line even run? Add console.log("got here");. If it never prints, the code never reached it.
  5. In Mad Libs, log what you read from the boxes right after you read it: console.log("adj:", adj);
  6. Clean up. Delete or comment out your debugging lines when you are done, especially alerts.

Try it: debug lab

Each program prints the wrong answer. Add a console.log or alert where you choose, run it, and read what it shows. Then click the line that has the bug. (Here, alert shows in the panel instead of popping up.)

6. Nothing happens? Check your file names

The browser loads the files your HTML names. If a name does not match a real file, exactly, that file silently does not load. Your functions do not exist, the page has no styling, or a picture is missing, and the only clue may be a 404 in the Console.

Files in the project
code.js
index.html
madlibs.jpeg
style.css
Inside index.html
<link rel="stylesheet" href="style.css"> matches
<script src="script.js"></script> no file named script.js
<img src="madlibs.jpg"> the file is .jpeg
In index.htmlMust match a file called
<script src="code.js">your JavaScript file
<link rel="stylesheet" href="style.css">your CSS file
<img src="madlibs.jpeg">each picture, including .jpg versus .jpeg
  • Capital letters count. Code.js and code.js are two different files on Codio.
  • Spaces and extra letters count. my code.js is not code.js.
  • The folder counts. A file in another folder needs the folder in the path.
  • Either rename the file or change the name in index.html. Just make them the same.

Click the file name cards in the lookup to see the real messages.

7. Debug steps: do these in order

  1. Save and reload. Are you looking at the newest version?
  2. Open the console and look at the first error only.
  3. Name the kind: SyntaxError (a typing or bracket mistake), TypeError or ReferenceError (the program started, then could not do something), or no error but a wrong result (a logic error).
  4. Go to the file and line in the message. Read that line, then the line above it.
  5. Check the pairs: every ( has a ), every { has a }, every [ has a ], every opening quote has a closing quote.
  6. Check the spelling and capitals of every id, variable, function, and file name against where it was created.
  7. Change one thing, then reload. If you change five things you will not know which one fixed it.
  8. No error but wrong answer? Add console.log(variable) to see what the code is really using.
  9. Still stuck? Explain the line out loud to a partner, one piece at a time. Or comment out half the code to find which half has the bug.

8. The four error types on the AP exam

TypeWhat it isIn this sheet
Syntax errorA mistake in the rules of the programming language. The program will not run at all.missing }, missing ), a missing quote, if age > 12
Run-time errorA mistake that happens while the program is running and stops it.TypeError and ReferenceError cards: reading from null, a name that does not exist
Logic errorThe program runs, but the algorithm is wrong, so the result is wrong. There is no error message.= instead of ===, a number read as text, ( ) instead of [ ]
Overflow errorThe computer tries to use a number outside the range it can store. For example, a number stored in 8 bits cannot be more than 255.Not in Mad Libs: JavaScript can hold huge numbers, but the AP exam asks about it