<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Craftstack Academy</title>
    <description>The latest articles on DEV Community by Craftstack Academy (@cstackacademy).</description>
    <link>https://dev.to/cstackacademy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4228756%2F9b623996-b69a-4f52-84a7-20dcb26a0469.jpg</url>
      <title>DEV Community: Craftstack Academy</title>
      <link>https://dev.to/cstackacademy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cstackacademy"/>
    <language>en</language>
    <item>
      <title>Python IndentationError: expected an indented block after 'if', 'for' or a function definition</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Fri, 20 Nov 2026 16:41:56 +0000</pubDate>
      <link>https://dev.to/cstackacademy/python-indentationerror-expected-an-indented-block-after-if-for-or-a-function-definition-bdk</link>
      <guid>https://dev.to/cstackacademy/python-indentationerror-expected-an-indented-block-after-if-for-or-a-function-definition-bdk</guid>
      <description>&lt;p&gt;You write your first &lt;code&gt;if&lt;/code&gt;: if the score is 50 or more, print "You passed". It looks right. You run it, and Python stops before it even starts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;  &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="s2"&gt;"main.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;^^^^^&lt;/span&gt;
&lt;span class="nc"&gt;IndentationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;indented&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="s1"&gt;'if'&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing is misspelled. The problem is two characters you can't see: the program is missing the spaces at the start of line 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the error means
&lt;/h2&gt;

&lt;p&gt;Read the last line first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IndentationError&lt;/code&gt;: the problem is the indentation, the spaces at the start of a line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expected an indented block&lt;/code&gt;: after a line that ends with a colon &lt;code&gt;:&lt;/code&gt;, Python expects one or more lines that are pushed in to the right. That pushed-in group is called a block.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;after 'if' statement on line 2&lt;/code&gt;: the line that opened the block is the &lt;code&gt;if&lt;/code&gt; on line 2. (Python 3.10 and newer add this part; older versions only say &lt;code&gt;expected an indented block&lt;/code&gt;.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The two lines above it say where Python noticed: line 3 of &lt;code&gt;main.py&lt;/code&gt;, and the &lt;code&gt;^&lt;/code&gt; marks point at the start of &lt;code&gt;print&lt;/code&gt;. Python reached line 3, saw that it wasn't indented, and stopped.&lt;/p&gt;

&lt;p&gt;Because this is found while Python reads your file, before anything runs, none of your program runs at all. Not even line 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Python cares about spaces
&lt;/h2&gt;

&lt;p&gt;Most languages mark a block with curly braces. In JavaScript, the same &lt;code&gt;if&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python has no braces. It uses indentation instead: every line that belongs to the &lt;code&gt;if&lt;/code&gt; is indented the same amount, and the first line that isn't indented any more is outside it. So in Python, indentation is not just tidiness; it is part of the meaning of the program. (If you're still choosing a first language, we compare this and more in &lt;a href="https://craftstackacademy.com/blog/which-programming-language-to-learn-first" rel="noopener noreferrer"&gt;Which programming language should you learn first?&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: indent the block
&lt;/h2&gt;

&lt;p&gt;Put four spaces in front of every line that belongs to the &lt;code&gt;if&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Well done!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Goodbye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You passed
Well done!
Goodbye
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both indented lines belong to the &lt;code&gt;if&lt;/code&gt;. &lt;code&gt;print("Goodbye")&lt;/code&gt; starts at the left edge again, so it is outside the block and always runs.&lt;/p&gt;

&lt;p&gt;Four spaces per level is the standard style in Python. Most code editors insert them for you: press Tab, or press Enter after a line that ends with &lt;code&gt;:&lt;/code&gt;, and the editor indents the next line.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same error after for, while and def
&lt;/h2&gt;

&lt;p&gt;Every line that ends with a colon opens a block: &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;def&lt;/code&gt; and others. The message names the one that is missing its block. With a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Ana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ben"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  File "main.py", line 2
    print(name)
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Ana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;  &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="s2"&gt;"main.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;^^^^^&lt;/span&gt;
&lt;span class="nc"&gt;IndentationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;indented&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;definition&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is the same each time: indent the lines that belong to it. Everything that should run on each pass of the loop, or every time the function is called, goes four spaces in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Ana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ben"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Ana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ana
Ben
Hi Ana
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;greet("Ana")&lt;/code&gt; is not indented: it's outside the function, and it's the line that calls it. If you indented it too, it would become part of the function, and nothing would ever call it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four related errors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  IndentationError: unexpected indent
&lt;/h3&gt;

&lt;p&gt;The opposite problem: a line is indented when nothing asked for a block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Ana"&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  File "main.py", line 2
    print(name)
IndentationError: unexpected indent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This often happens when you paste code from somewhere else, or press space by accident. Move the line back to the left edge.&lt;/p&gt;

&lt;h3&gt;
  
  
  IndentationError: unindent does not match any outer indentation level
&lt;/h3&gt;

&lt;p&gt;The lines in a block must all line up. Here the second &lt;code&gt;print&lt;/code&gt; is indented by two spaces, while the first one has four:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Well done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;  &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="s2"&gt;"main.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Well done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                      &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="nc"&gt;IndentationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;unindent&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nb"&gt;any&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt; &lt;span class="n"&gt;indentation&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python can't tell whether line 4 is meant to be inside the &lt;code&gt;if&lt;/code&gt; (four spaces) or outside it (no spaces). Choose one and line it up exactly.&lt;/p&gt;

&lt;h3&gt;
  
  
  TabError: inconsistent use of tabs and spaces in indentation
&lt;/h3&gt;

&lt;p&gt;A Tab character and four spaces can look identical on screen, but to Python they are different. If one line in a block is indented with spaces and the next with a Tab, you get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;TabError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inconsistent&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;tabs&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;indentation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cure is to use only spaces. Most editors have a setting to insert spaces when you press Tab, and a command to turn existing Tabs into spaces.&lt;/p&gt;

&lt;h3&gt;
  
  
  A block with only a comment in it
&lt;/h3&gt;

&lt;p&gt;Sometimes you start an &lt;code&gt;if&lt;/code&gt; and want to fill it in later, so you leave a comment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# TODO&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;IndentationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;indented&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="s1"&gt;'if'&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A comment doesn't count as code, so the block is still empty. Write &lt;code&gt;pass&lt;/code&gt;, which means "do nothing here for now":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The sneaky one: no error, wrong result
&lt;/h2&gt;

&lt;p&gt;Indentation can also be wrong without any error at all. Look at this version with a score of 40:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"You passed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Well done!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Well done!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python is happy, but the program congratulates someone who failed. &lt;code&gt;print("Well done!")&lt;/code&gt; is not indented, so it isn't part of the &lt;code&gt;if&lt;/code&gt; and runs every time. When a program does something "even though" a condition is false, check the indentation of that line first.&lt;/p&gt;

&lt;p&gt;The same goes for &lt;code&gt;else&lt;/code&gt;: it has to line up with its &lt;code&gt;if&lt;/code&gt;. Indent it by mistake, and Python reports a &lt;code&gt;SyntaxError: invalid syntax&lt;/code&gt; on the &lt;code&gt;else&lt;/code&gt; line.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Find the line number in the message, and look at the line above it: does it end with a colon?&lt;/li&gt;
&lt;li&gt;Indent every line that belongs to that block by four spaces.&lt;/li&gt;
&lt;li&gt;Make sure all lines in a block line up exactly, and that &lt;code&gt;else&lt;/code&gt; and &lt;code&gt;elif&lt;/code&gt; line up with their &lt;code&gt;if&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use spaces only, never Tab characters. Let your editor handle it.&lt;/li&gt;
&lt;li&gt;If the program runs but does the wrong thing, check which lines are inside the block and which are outside.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In our course "The Toolkit", indentation comes up in the very first session, where you build a program that turns a score into a letter grade with &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt;. When you meet this error, the tutor doesn't fix it for you. It starts with a small hint, like asking which line the message points to and what the line above it ends with, then a more concrete hint, and shows the full solution only after two real attempts, so the fix is usually one you found yourself.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://craftstackacademy.com/blog/python-indentationerror-expected-an-indented-block" rel="noopener noreferrer"&gt;Craftstack Academy&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>debugging</category>
      <category>programming</category>
    </item>
    <item>
      <title>Which programming language should you learn first? Python, JavaScript, SQL or Java</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Thu, 19 Nov 2026 10:36:23 +0000</pubDate>
      <link>https://dev.to/cstackacademy/which-programming-language-should-you-learn-first-python-javascript-sql-or-java-427i</link>
      <guid>https://dev.to/cstackacademy/which-programming-language-should-you-learn-first-python-javascript-sql-or-java-427i</guid>
      <description>&lt;p&gt;You've decided to learn to code. You search for where to start, and within ten minutes you have read forty opinions, three of them angry. One person says Python, one says JavaScript, one says "it doesn't matter", and someone else says you should start with C because it builds character.&lt;/p&gt;

&lt;p&gt;This guide is for the moment before your first line of code. It compares the four languages most beginners choose between, Python, JavaScript, SQL and Java, using the same small program in each, so you can see the differences instead of just reading about them. Every program in this article was run, and each prints the same result.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short answer
&lt;/h2&gt;

&lt;p&gt;If you only read one section, read this one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You want to build websites, or you want to see your work on a page you can open and share:&lt;/strong&gt; start with HTML, CSS and JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You want one language for a bit of everything: automating boring tasks, working with data, AI, small tools:&lt;/strong&gt; start with Python.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You work with spreadsheets, reports or any kind of data:&lt;/strong&gt; learn SQL, on its own or next to one of the others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You study computer science, want to work on large back-end systems, or like strict rules:&lt;/strong&gt; Java is a strong first language, if you don't mind more typing at the start.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You still can't decide:&lt;/strong&gt; pick Python. In the 2025 Stack Overflow Developer Survey, 71.8% of people learning to code said they had used Python in the past year, more than any other language. Nothing you learn in it is wasted when you move on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest of this guide explains why, and shows what each language actually looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  What matters more than the language
&lt;/h2&gt;

&lt;p&gt;Before the languages themselves, four things decide whether you keep going far more than which language you picked:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A goal you care about.&lt;/strong&gt; "Learn to code" is hard to stick with. "Build a page for my photos" or "stop copying numbers between spreadsheets by hand" is much easier, because you know when you've got there. Pick the language that leads to your goal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Seeing a result quickly.&lt;/strong&gt; Most beginners who give up do it early, when it takes a whole evening to get anything on screen. A language and a setup that show you a result in the first ten minutes keep you going.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing to install at the start.&lt;/strong&gt; Installing tools, setting paths and fixing versions is a classic place to get stuck before you have written a single line. Start in the browser, and install things once you know you like it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Writing the code yourself.&lt;/strong&gt; Watching videos feels like progress, but you learn to program by typing code, running it and fixing what goes wrong. Ten minutes of your own code teaches more than an hour of watching someone else's.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One more thing: the ideas carry over. Variables, conditions, loops, functions and reading error messages work almost the same way in every language. Your first language is not a lifelong choice; it is where you learn to think like a programmer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same program in four languages
&lt;/h2&gt;

&lt;p&gt;To compare the languages fairly, we wrote one small program in each. It starts with five quiz scores and works out three things: how many scores there are, their average, and the best one.&lt;/p&gt;

&lt;p&gt;The scores are 72, 85, 90, 64 and 88. Each version prints 5 scores, an average of 79.8 and a best score of 90. Look at how each language gets there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python: the all-rounder
&lt;/h3&gt;

&lt;p&gt;Python is a general-purpose language: people use it for automation scripts, data analysis, AI and machine learning, the back end of websites, and science. If you have ever wished a computer would just do a boring task for you, Python is usually the quickest way to make that happen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# scores.py&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Scores:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Average:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Best:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scores: 5
Average: 79.8
Best: 90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It reads almost like English. &lt;code&gt;sum(scores)&lt;/code&gt;, &lt;code&gt;len(scores)&lt;/code&gt; and &lt;code&gt;max(scores)&lt;/code&gt; do exactly what their names say.&lt;/li&gt;
&lt;li&gt;There is very little punctuation: no semicolons, no curly braces, and you don't have to say in advance what kind of value each variable holds.&lt;/li&gt;
&lt;li&gt;Python uses indentation (the spaces at the start of a line) to group code. That feels strict at first, but it trains you to write tidy code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The catch:&lt;/strong&gt; because Python doesn't check the types of your values before it runs, some mistakes only show up when the line runs. The classic one is mixing text and numbers, which we explain in &lt;a href="https://craftstackacademy.com/blog/python-typeerror-unsupported-operand-int-and-str" rel="noopener noreferrer"&gt;Python TypeError: unsupported operand type(s) for -: 'int' and 'str'&lt;/a&gt;. And Python is not what runs inside a web page: if you want to build websites, you'll still need HTML, CSS and JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who it suits:&lt;/strong&gt; almost everyone who doesn't yet know what they want to build. In the 2025 survey, 57.9% of all respondents had used Python in the past year, up 7 percentage points from 2024.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it:&lt;/strong&gt; our course Python from Scratch takes you from your first &lt;code&gt;print()&lt;/code&gt; to a small program with files and classes in 12 one-hour sessions (three courses of four). The free trial, "Your Life in Numbers", has you write a program that asks for your birth year and works out how many days you have lived. Choose it on the &lt;a href="https://craftstackacademy.com/trial" rel="noopener noreferrer"&gt;trial page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript, with HTML and CSS: the language of the web
&lt;/h3&gt;

&lt;p&gt;Every web page is built from three layers. HTML is the structure: headings, paragraphs, buttons. CSS is the look: colours, fonts, layout. JavaScript is the behaviour: what happens when you click, type or scroll. HTML and CSS are not programming languages in the strict sense, but they come as a set with JavaScript, and they are the friendliest possible start: you change a line and see the page change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scores.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Scores:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Average:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Best:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scores: 5
Average: 79.8
Best: 90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can run this in any browser: open the developer tools (F12 in most browsers), go to the Console tab and paste it in.&lt;/p&gt;

&lt;p&gt;What to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript has no built-in &lt;code&gt;sum()&lt;/code&gt;, so the program adds the scores up with a loop: &lt;code&gt;for (const s of scores)&lt;/code&gt; means "for each score in the list".&lt;/li&gt;
&lt;li&gt;Curly braces &lt;code&gt;{ }&lt;/code&gt; group the code, and lines usually end with a semicolon.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Math.max(...scores)&lt;/code&gt; finds the best score. The three dots spread the list out into separate values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The catch:&lt;/strong&gt; JavaScript tries hard not to stop with an error, and sometimes that surprises you. &lt;code&gt;"5" + 3&lt;/code&gt; gives &lt;code&gt;"53"&lt;/code&gt; (it joins them as text), while &lt;code&gt;"5" - 3&lt;/code&gt; gives &lt;code&gt;2&lt;/code&gt;. Python would stop and tell you that you mixed text and a number. The other trap is the sheer number of tools and frameworks around JavaScript. Ignore them at first: plain HTML, CSS and JavaScript are enough for a long time. When something goes wrong on a page, the error you'll meet first is often the one in &lt;a href="https://craftstackacademy.com/blog/javascript-cannot-read-properties-of-null-addeventlistener" rel="noopener noreferrer"&gt;Cannot read properties of null (reading 'addEventListener')&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who it suits:&lt;/strong&gt; anyone who wants to build websites or web apps, or who learns best by seeing results. It is also the most used language overall: in the 2025 survey, 66% of respondents had used JavaScript and 61.9% HTML and CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it:&lt;/strong&gt; The Front-End Foundation has three courses: HTML and CSS first, then JavaScript and the page (the DOM) with a capstone project, then TypeScript. The free trial, "Your Card on the Web", has you build a profile card with HTML and CSS, styled like a modern website. Choose it on the &lt;a href="https://craftstackacademy.com/trial" rel="noopener noreferrer"&gt;trial page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  SQL: the language of data
&lt;/h3&gt;

&lt;p&gt;SQL works differently from the other three. In Python, JavaScript and Java you tell the computer &lt;em&gt;how&lt;/em&gt; to do something, step by step. In SQL you describe &lt;em&gt;what&lt;/em&gt; you want, and the database works out how to get it. That makes SQL a query language rather than a general-purpose one: you won't build an app in SQL alone, but almost every app keeps its data in a database that speaks it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- scores.sql&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ana'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ben'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cara'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Dan'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Eva'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS scores, AVG(score) AS average, MAX(score) AS best
FROM scores;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scores  average  best
------  -------  ----
5       79.8     90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first two statements only set up the example: a table with a row for each student. The question itself is the last statement, and it reads almost like a sentence: count the rows, average the scores, take the highest, from the scores table.&lt;/li&gt;
&lt;li&gt;There is no loop and no total to keep. You say what you want, and the database does the work.&lt;/li&gt;
&lt;li&gt;Because each score belongs to a named student, you could ask much more with one more line, like who got the best score, or everyone above the average.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The catch:&lt;/strong&gt; SQL on its own doesn't make programs, so sooner or later you'll pair it with another language. There are a few versions (SQLite, PostgreSQL, MySQL and others), but the basics you learn first are the same in all of them. The first error most people meet is a word without quotes, explained in &lt;a href="https://craftstackacademy.com/blog/sqlite-no-such-column-error" rel="noopener noreferrer"&gt;SQL error "no such column" in SQLite&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who it suits:&lt;/strong&gt; not only programmers. Analysts, marketers, product managers and people in finance use SQL to answer their own questions instead of waiting for someone else. In the 2025 survey, 58.6% of respondents had used SQL, slightly more than Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it:&lt;/strong&gt; Databases &amp;amp; SQL Fundamentals goes in five sessions from what a database actually is to designing tables and writing real queries with joins. The free trial, "The Museum Mystery", starts with a missing diamond: you search the visitor log with SQL, clue by clue, until only the thief is left. Choose it on the &lt;a href="https://craftstackacademy.com/trial" rel="noopener noreferrer"&gt;trial page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java: the strict teacher
&lt;/h3&gt;

&lt;p&gt;Java runs a huge amount of the world's software: the back-end systems of many banks, shops and airlines, and a lot of Android apps. (Google now recommends Kotlin for new Android apps, but Java is still supported, and a lot of existing Android code is written in it.) It is also a favourite at universities, and the US high-school course AP Computer Science A is taught in Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Scores.java&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Scores&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Scores: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Average: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Best: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scores: 5
Average: 79.8
Best: 90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is more to type. In the classic form you'll see almost everywhere, every program lives in a class and starts in a method called &lt;code&gt;main&lt;/code&gt;. For now, that's just the frame around your code. (Since Java 25, very small programs may leave out the class line, but most code and courses still use this form.)&lt;/li&gt;
&lt;li&gt;You say what kind of value each variable holds: &lt;code&gt;int[]&lt;/code&gt; is a list of whole numbers, &lt;code&gt;int&lt;/code&gt; a whole number, &lt;code&gt;double&lt;/code&gt; a number with a decimal point.&lt;/li&gt;
&lt;li&gt;Look at &lt;code&gt;(double)&lt;/code&gt;. Without it, Java divides one whole number by another and keeps only the whole part, so the average comes out as 79.0:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 399 / 5 gives 79.0, not 79.8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is exactly the kind of detail Java makes you think about, and why people say it teaches you how things really work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The catch:&lt;/strong&gt; you type more before your first result, and the program has to be compiled (checked and translated) before it runs. The compiler's messages can look scary at first, but they point to the exact line and catch many mistakes before your program ever runs. The one almost everyone meets early is &lt;a href="https://craftstackacademy.com/blog/java-incompatible-types-string-cannot-be-converted-to-int" rel="noopener noreferrer"&gt;incompatible types: String cannot be converted to int&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who it suits:&lt;/strong&gt; students, people heading for software jobs at large companies, and anyone who likes clear rules. In the 2025 survey, 29.4% of all respondents had used Java, and 40.8% of those learning to code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it:&lt;/strong&gt; Java Mastery has two courses, eight sessions in all, from your first compiled program to classes, interfaces, generics and streams. The free trial, "Your Year on Screen", has you write a program that asks how many hours a day you spend on your phone and works out how many full days a year that adds up to. Java runs right in your browser, so there is nothing to install. Choose it on the &lt;a href="https://craftstackacademy.com/trial" rel="noopener noreferrer"&gt;trial page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four side by side
&lt;/h2&gt;

&lt;p&gt;Here is how the four compare on the things that matter most when you start:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Easiest first steps:&lt;/strong&gt; Python, and HTML with CSS. JavaScript and SQL come close; Java asks for the most before your first result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fastest visible result:&lt;/strong&gt; HTML, CSS and JavaScript. You get a page you can open and show people.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most useful outside programming jobs:&lt;/strong&gt; SQL for anyone who works with data, and Python for automating everyday tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built into every web browser:&lt;/strong&gt; JavaScript, and only JavaScript of these four. (Python, SQL and Java can run in a browser too, with extra tools loaded by the page, which is how our trials work.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Catches mistakes before the program runs:&lt;/strong&gt; Java, thanks to its compiler. (TypeScript, which you can learn after JavaScript, adds the same kind of checking to the web.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shortest version of our program:&lt;/strong&gt; Python, with SQL a close second if you only count the question.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most used by people learning to code:&lt;/strong&gt; Python, then HTML and CSS, then JavaScript, SQL and Java.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Myths that keep people from starting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  "You need to be good at maths"
&lt;/h3&gt;

&lt;p&gt;For most beginner programs, the maths you already know is enough. The average in this article is the hardest maths you'll see here. Some areas, like games, graphics and machine learning, use more maths, and you can learn that when you get there. Programming is much more about breaking a problem into small steps than about calculating.&lt;/p&gt;

&lt;h3&gt;
  
  
  "You have to find the best language"
&lt;/h3&gt;

&lt;p&gt;There is no best language, only the best one for your goal. Professional programmers use several, and they usually picked up the second and third much faster than the first, because the ideas were already there.&lt;/p&gt;

&lt;h3&gt;
  
  
  "If you switch later, you start from zero"
&lt;/h3&gt;

&lt;p&gt;Look again at the four programs above. They all do the same thing: keep a list, add it up, find the largest value. The spelling changes, the thinking doesn't. Moving from Python to JavaScript, or from JavaScript to Java, feels like learning a new accent, not a new language.&lt;/p&gt;

&lt;h3&gt;
  
  
  "You need a powerful computer and a complicated setup"
&lt;/h3&gt;

&lt;p&gt;Any laptop with a modern browser is enough to start. You can write and run Python, web pages, SQL and even Java in the browser; install proper tools once you know you want to keep going.&lt;/p&gt;

&lt;h3&gt;
  
  
  "AI writes the code now, so why learn?"
&lt;/h3&gt;

&lt;p&gt;AI tools are genuinely useful, but someone still has to read the code they produce, judge whether it's right and fix it when it isn't. If you let AI write everything while you learn, you end up with code you can't read. The best use of AI as a beginner is as a tutor that asks you questions and gives hints, not one that hands over the answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to decide in 20 minutes
&lt;/h2&gt;

&lt;p&gt;You can read comparisons for weeks. A faster way is to try:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pick your goal from the short answer at the top of this article.&lt;/li&gt;
&lt;li&gt;Write one small, real program in the matching language. Not a video, not a quiz, but code you type and run yourself.&lt;/li&gt;
&lt;li&gt;Notice how it felt. Did you want to change something and run it again? That curiosity is the best sign you have picked well.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is exactly what our free trials are for. Each one is 20 minutes, one-on-one with an AI tutor, in Python, a web page, SQL or Java, right in your browser, with no card and nothing to install. The tutor reads your real code and output, and when something breaks, it asks questions and gives hints instead of handing you the fix. You can take all four, each once, so start with the language you're most curious about.&lt;/p&gt;

&lt;p&gt;Not sure even after reading this? When you sign in on the &lt;a href="https://craftstackacademy.com/trial" rel="noopener noreferrer"&gt;trial page&lt;/a&gt;, you answer four short questions (what you want to learn, how much you've coded, how much time you have and why you're learning), and we recommend a course and the session to start with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Is Python or JavaScript better for a complete beginner?
&lt;/h3&gt;

&lt;p&gt;Both are good first languages. Python is a little easier to read and stops you with a clear error when you mix things up. JavaScript shows results on a web page straight away. If you want to build websites, start with HTML, CSS and JavaScript; for anything else, or if you're unsure, start with Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I learn SQL as my first language?
&lt;/h3&gt;

&lt;p&gt;Yes. SQL is small, very practical and quick to get useful with, especially if your job involves data. Just know that it answers questions about data rather than building programs, so you'll probably add Python or JavaScript later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is Java too hard to start with?
&lt;/h3&gt;

&lt;p&gt;No, just slower to get going. You type more before your first result, and the compiler insists on details that Python lets slide. In return, you understand types and structure early, which makes later languages easier. If you're studying computer science, starting with Java is a sensible choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  How long until I can build something real?
&lt;/h3&gt;

&lt;p&gt;It depends on how much time you put in, but here is a realistic picture from our own courses. After the first four one-hour sessions of Python, you write small working scripts on your own. After twelve, you have finished a project of your own that works with files and classes. On the web path, you build an interactive page in the capstone of the second course, after eight sessions. With one session a week, twelve sessions is about three months.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I learn two languages at once?
&lt;/h3&gt;

&lt;p&gt;Usually not at the start. The exceptions are HTML, CSS and JavaScript, which belong together, and SQL, which pairs well with Python once you know the basics of one of them. Otherwise, get comfortable with one language first. The second will come much faster.&lt;/p&gt;

&lt;p&gt;Whichever you choose, the most important step is the first program you write yourself. Pick a language, write a few lines, and run them today.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://craftstackacademy.com/blog/which-programming-language-to-learn-first" rel="noopener noreferrer"&gt;Craftstack Academy&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Java: incompatible types: String cannot be converted to int, and how to fix it</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Wed, 18 Nov 2026 11:07:02 +0000</pubDate>
      <link>https://dev.to/cstackacademy/java-incompatible-types-string-cannot-be-converted-to-int-and-how-to-fix-it-3f53</link>
      <guid>https://dev.to/cstackacademy/java-incompatible-types-string-cannot-be-converted-to-int-and-how-to-fix-it-3f53</guid>
      <description>&lt;p&gt;You press compile, and before your program even starts, Java refuses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main.java:7: error: incompatible types: String cannot be converted to int
        int hours = scanner.nextLine();
                                    ^
1 error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The good news: this is a compile error. Java found the problem before running anything, and it tells you the exact file, line and spot. Here's what it means, the usual causes, how to fix each one, and the related errors you're likely to meet next.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the error means
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Main.java:7&lt;/code&gt;: the file and the line number.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;incompatible types&lt;/code&gt;: you put a value of one type into a place meant for another type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String cannot be converted to int&lt;/code&gt;: the value is a &lt;code&gt;String&lt;/code&gt; (text), and the place, here the variable &lt;code&gt;hours&lt;/code&gt;, is an &lt;code&gt;int&lt;/code&gt; (a whole number). Java won't turn one into the other on its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;^&lt;/code&gt; mark shows where Java noticed it: at the end of &lt;code&gt;scanner.nextLine()&lt;/code&gt;, the expression that produced the text.&lt;/p&gt;

&lt;p&gt;Java checks types very strictly. A variable declared as &lt;code&gt;int&lt;/code&gt; can only ever hold a whole number, so Java stops you as soon as you try to put text in it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same error with java.lang.String
&lt;/h2&gt;

&lt;p&gt;Depending on your editor, the same message can look a little different, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;java.lang.String&lt;/code&gt; is simply the full name of &lt;code&gt;String&lt;/code&gt; (the class lives in the package &lt;code&gt;java.lang&lt;/code&gt;), and the &lt;code&gt;java:&lt;/code&gt; at the start only says that the message comes from the Java compiler. It's the same error, with the same causes and the same fixes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: reading a number with nextLine()
&lt;/h2&gt;

&lt;p&gt;This is the most common one. You ask a question with a &lt;code&gt;Scanner&lt;/code&gt; and want the answer as a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"How many hours a day are you on your phone? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"That is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;365&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" hours a year."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;scanner.nextLine()&lt;/code&gt; always gives back the whole line the user typed as a &lt;code&gt;String&lt;/code&gt;, even if they typed only digits like &lt;code&gt;3&lt;/code&gt;. The text &lt;code&gt;"3"&lt;/code&gt; isn't the number 3, so it can't go into an &lt;code&gt;int&lt;/code&gt;. The same goes for &lt;code&gt;scanner.next()&lt;/code&gt;, which reads one word: it also gives back a &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The fix: Integer.parseInt
&lt;/h3&gt;

&lt;p&gt;Read the line as text, then convert it with &lt;code&gt;Integer.parseInt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"How many hours a day are you on your phone? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"That is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;365&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" hours a year."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How many hours a day are you on your phone? 3
That is 1095 hours a year.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it from the inside out: &lt;code&gt;nextLine()&lt;/code&gt; gives the text &lt;code&gt;"3"&lt;/code&gt;, &lt;code&gt;Integer.parseInt&lt;/code&gt; turns it into the number 3, and that number goes into &lt;code&gt;hours&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about nextInt()?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Scanner&lt;/code&gt; also has &lt;code&gt;nextInt()&lt;/code&gt;, which reads a number directly, so it seems like the simpler fix. It works, but it has a trap that catches almost everyone. Ask for a number with &lt;code&gt;nextInt()&lt;/code&gt; and then for a name with &lt;code&gt;nextLine()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hours a day? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Your name? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Name: ["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"], hours: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type &lt;code&gt;3&lt;/code&gt;, press Enter, and the program doesn't wait for your name at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hours a day? 3
Your name? Name: [], hours: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;nextInt()&lt;/code&gt; reads the 3 but leaves the Enter you pressed behind. The next &lt;code&gt;nextLine()&lt;/code&gt; finds that leftover line break straight away and returns an empty line. That's why reading every answer with &lt;code&gt;nextLine()&lt;/code&gt; and converting with &lt;code&gt;Integer.parseInt&lt;/code&gt; is the safer habit. (If you do use &lt;code&gt;nextInt()&lt;/code&gt;, call &lt;code&gt;scanner.nextLine();&lt;/code&gt; once right after it to throw away the rest of the line.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: a value in quotes
&lt;/h2&gt;

&lt;p&gt;The same error appears when you write a value in double quotes by mistake. Anything in double quotes is a &lt;code&gt;String&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"thirty"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"thirty"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;^&lt;/code&gt; points at the text itself. The fix depends on what you meant: a number goes without quotes, &lt;code&gt;int age = 30;&lt;/code&gt;, and if you really want text, the variable must be a &lt;code&gt;String&lt;/code&gt;: &lt;code&gt;String age = "thirty";&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: a method that returns a String
&lt;/h2&gt;

&lt;p&gt;A method's return type decides what its result is, wherever you use it. If a method is declared to return a &lt;code&gt;String&lt;/code&gt;, you can't put its result into an &lt;code&gt;int&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;askName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Lena"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;askName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;askName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                           &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the method's first line, &lt;code&gt;static String askName()&lt;/code&gt;: the &lt;code&gt;String&lt;/code&gt; before the name is what it gives back. Either store the result in a &lt;code&gt;String&lt;/code&gt;, or, if the method should really give a number, change its return type and what it returns.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can happen next: NumberFormatException
&lt;/h2&gt;

&lt;p&gt;Once the program compiles, try typing &lt;code&gt;2.5&lt;/code&gt; as the answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How many hours a day are you on your phone? 2.5
Exception in thread "main" java.lang.NumberFormatException: For input string: "2.5"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:668)
    at java.base/java.lang.Integer.parseInt(Integer.java:786)
    at Main.main(Main.java:7)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a different kind of problem. The code compiled fine, but while running, &lt;code&gt;Integer.parseInt&lt;/code&gt; got text that isn't a whole number. That's called an exception, and the last &lt;code&gt;at&lt;/code&gt; line shows where it came from in your code: &lt;code&gt;Main.java:7&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If decimals are allowed, use a &lt;code&gt;double&lt;/code&gt; and &lt;code&gt;Double.parseDouble&lt;/code&gt; instead.&lt;/li&gt;
&lt;li&gt;If you need a whole number, catch the exception and ask nicely:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Main.java&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"How many hours a day are you on your phone? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"That is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;365&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" hours a year."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NumberFormatException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Please type a whole number, like 3."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How many hours a day are you on your phone? 2.5
Please type a whole number, like 3.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Related errors
&lt;/h2&gt;

&lt;p&gt;The same "incompatible types" message comes in a few other flavours. Each one names the two types, so read them the same way: the value's type first, then the type of the place it's going.&lt;/p&gt;

&lt;h3&gt;
  
  
  int cannot be converted to String
&lt;/h3&gt;

&lt;p&gt;The opposite direction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                       &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Turn the number into text with &lt;code&gt;String.valueOf(hours)&lt;/code&gt;, or join it to some text with &lt;code&gt;+&lt;/code&gt;, which makes the whole thing a &lt;code&gt;String&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// "3"&lt;/span&gt;
&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hours: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// "Hours: 3"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  String cannot be converted to double
&lt;/h3&gt;

&lt;p&gt;The same problem with a decimal number, for example &lt;code&gt;double price = scanner.nextLine();&lt;/code&gt;. The fix is the decimal version of &lt;code&gt;parseInt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  String cannot be converted to char
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                     &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Java, double quotes make a &lt;code&gt;String&lt;/code&gt;, and single quotes make a single character, a &lt;code&gt;char&lt;/code&gt;. Write &lt;code&gt;char grade = 'A';&lt;/code&gt;. If you have a &lt;code&gt;String&lt;/code&gt; and want its first character, use &lt;code&gt;text.charAt(0)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  possible lossy conversion from double to int
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;possible&lt;/span&gt; &lt;span class="n"&gt;lossy&lt;/span&gt; &lt;span class="n"&gt;conversion&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;7 / 2.0&lt;/code&gt; is &lt;code&gt;3.5&lt;/code&gt;, a &lt;code&gt;double&lt;/code&gt;, and putting it into an &lt;code&gt;int&lt;/code&gt; would lose the &lt;code&gt;.5&lt;/code&gt;. Java won't do that silently. Either keep the decimals with &lt;code&gt;double average = total / 2.0;&lt;/code&gt;, or say that you really want to cut them off with a cast: &lt;code&gt;int average = (int) (total / 2.0);&lt;/code&gt;, which gives 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compile errors and exceptions
&lt;/h2&gt;

&lt;p&gt;It helps to keep the two apart:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;compile error&lt;/strong&gt; like &lt;code&gt;incompatible types&lt;/code&gt; stops &lt;code&gt;javac&lt;/code&gt; before anything runs. It always has &lt;code&gt;error:&lt;/code&gt; and a line number in your file.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;exception&lt;/strong&gt; like &lt;code&gt;NumberFormatException&lt;/code&gt; happens while the program runs, often because of the input. It starts with &lt;code&gt;Exception in thread "main"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both point you to a line in your code. Start there. For the exception you'll probably meet soon after this one, see &lt;a href="https://craftstackacademy.com/blog/java-nullpointerexception-cannot-invoke-because-is-null" rel="noopener noreferrer"&gt;Java NullPointerException&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Read the two types in the message: the first is the value you have, the second is what the variable (or parameter) expects.&lt;/li&gt;
&lt;li&gt;Find where the value comes from: &lt;code&gt;nextLine()&lt;/code&gt; and &lt;code&gt;next()&lt;/code&gt; always give a &lt;code&gt;String&lt;/code&gt;, so does anything in double quotes, and so does a method declared to return &lt;code&gt;String&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Convert on purpose: &lt;code&gt;Integer.parseInt&lt;/code&gt; or &lt;code&gt;Double.parseDouble&lt;/code&gt; for text to numbers, &lt;code&gt;String.valueOf&lt;/code&gt; for numbers to text, single quotes for a &lt;code&gt;char&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Prefer &lt;code&gt;nextLine()&lt;/code&gt; plus &lt;code&gt;parseInt&lt;/code&gt; over &lt;code&gt;nextInt()&lt;/code&gt;, or clear the line after &lt;code&gt;nextInt()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once it compiles, test with input you didn't expect, like &lt;code&gt;2.5&lt;/code&gt; or a word, and handle the &lt;code&gt;NumberFormatException&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This error is part of our free Java trial, "Your Year on Screen": you read an answer with &lt;code&gt;scanner.nextLine()&lt;/code&gt; into an &lt;code&gt;int&lt;/code&gt;, and the compiler stops you. The tutor doesn't hand you &lt;code&gt;Integer.parseInt&lt;/code&gt;. It first asks you to read the error and say which two types it talks about, and gives the next hint only if you need it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://craftstackacademy.com/blog/java-incompatible-types-string-cannot-be-converted-to-int" rel="noopener noreferrer"&gt;Craftstack Academy&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>debugging</category>
      <category>programming</category>
    </item>
    <item>
      <title>Python TypeError: unsupported operand type(s) for -: 'int' and 'str' (or 'str' and 'int')</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Tue, 17 Nov 2026 09:03:52 +0000</pubDate>
      <link>https://dev.to/cstackacademy/python-typeerror-unsupported-operand-types-for-int-and-str-or-str-and-int-pd5</link>
      <guid>https://dev.to/cstackacademy/python-typeerror-unsupported-operand-types-for-int-and-str-or-str-and-int-pd5</guid>
      <description>&lt;p&gt;You wrote a short program that asks for your birth year and works out your age. You type 2002, press Enter, and Python stops with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traceback (most recent call last):
  File "main.py", line 3, in &amp;lt;module&amp;gt;
    age = 2026 - year
          ~~~~~^~~~~~~
TypeError: unsupported operand type(s) for -: 'int' and 'str'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the most common errors in a first Python program. The fix is one word, but it's worth understanding why it happens, because the same idea comes back again and again.&lt;/p&gt;

&lt;p&gt;Here is the program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Your name? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Born in? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"- you are"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What the error means
&lt;/h2&gt;

&lt;p&gt;Read the last line of the error from the left:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;TypeError&lt;/code&gt;: the problem is the type of a value, not its spelling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;unsupported operand type(s) for -&lt;/code&gt;: the minus sign got values it can't work with.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'int' and 'str'&lt;/code&gt;: the value on the left is an &lt;code&gt;int&lt;/code&gt; (a whole number, &lt;code&gt;2026&lt;/code&gt;) and the value on the right is a &lt;code&gt;str&lt;/code&gt; (a string, which means text).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python can subtract a number from a number. It can't subtract text from a number, and it doesn't guess that the text &lt;code&gt;"2002"&lt;/code&gt; was meant as a number.&lt;/p&gt;

&lt;p&gt;The lines above the last one tell you where it happened: line 3 of &lt;code&gt;main.py&lt;/code&gt;. The &lt;code&gt;^&lt;/code&gt; mark points at the minus sign. (Python 3.11 and newer draw that mark; older versions only show the line.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Why year is text
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;input()&lt;/code&gt; always gives back a string, even when you type only digits. You can check it yourself with &lt;code&gt;type()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Born in? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Born in? 2002
&amp;lt;class 'str'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;year&lt;/code&gt; holds the text &lt;code&gt;"2002"&lt;/code&gt;, not the number 2002. To Python those are as different as the word "five" and the number 5.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: turn the text into a number
&lt;/h2&gt;

&lt;p&gt;Wrap the &lt;code&gt;input()&lt;/code&gt; in &lt;code&gt;int()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Your name? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Born in? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"- you are"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your name? Lena
Born in? 2002
Hi Lena - you are 24
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the new line from the inside out: &lt;code&gt;input()&lt;/code&gt; asks the question and gives back text, then &lt;code&gt;int()&lt;/code&gt; turns that text into a whole number. Now both sides of the minus sign are numbers.&lt;/p&gt;

&lt;p&gt;For numbers with a decimal point, like a height of 1.75, use &lt;code&gt;float()&lt;/code&gt; instead of &lt;code&gt;int()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same error with other operators, and the other order
&lt;/h2&gt;

&lt;p&gt;The two types in the message are in the same order as in your code: the value left of the operator first, then the value right of it. So if the text is on the left, the message turns round:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"2002"&lt;/span&gt;
&lt;span class="n"&gt;next_birthday&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: unsupported operand type(s) for -: 'str' and 'int'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The symbol after &lt;code&gt;for&lt;/code&gt; tells you which operator failed. With text on the left, you'll see the same message for division and floor division, and a slightly longer one for powers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: unsupported operand type(s) for /: 'str' and 'int'
TypeError: unsupported operand type(s) for //: 'str' and 'int'
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with a number on the left of a &lt;code&gt;+&lt;/code&gt;, like &lt;code&gt;2026 + year&lt;/code&gt;, it's &lt;code&gt;for +: 'int' and 'str'&lt;/code&gt;. You get the same message from &lt;code&gt;sum()&lt;/code&gt; on a list of strings, such as &lt;code&gt;sum(["72", "85"])&lt;/code&gt;, because &lt;code&gt;sum&lt;/code&gt; adds the items up one by one, starting from the number 0.&lt;/p&gt;

&lt;p&gt;The fix is the same every time: convert the text to a number before you calculate, with &lt;code&gt;int()&lt;/code&gt; or &lt;code&gt;float()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Three operators behave differently, so watch out for them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;year + 1&lt;/code&gt; with text on the left gives &lt;code&gt;can only concatenate str (not "int") to str&lt;/code&gt; (see below).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;year % 2&lt;/code&gt; gives the puzzling &lt;code&gt;TypeError: not all arguments converted during string formatting&lt;/code&gt;, because &lt;code&gt;%&lt;/code&gt; after a string means "fill in this text", not "remainder".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;year * 2&lt;/code&gt; gives no error at all: it prints &lt;code&gt;20022002&lt;/code&gt;. Multiplying text by a whole number repeats the text. (Two strings, like &lt;code&gt;"5" * "3"&lt;/code&gt;, do fail: &lt;code&gt;can't multiply sequence by non-int of type 'str'&lt;/code&gt;.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the message says &lt;code&gt;'NoneType'&lt;/code&gt; instead of &lt;code&gt;'str'&lt;/code&gt;, the value isn't text but nothing: often a function that prints its result instead of returning it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two errors you may meet next
&lt;/h2&gt;

&lt;h3&gt;
  
  
  can only concatenate str (not "int") to str
&lt;/h3&gt;

&lt;p&gt;This is the same problem the other way round. &lt;code&gt;+&lt;/code&gt; between two strings joins them, but here one side is a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Born in "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2002&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Either let &lt;code&gt;print()&lt;/code&gt; put the pieces together with commas, &lt;code&gt;print("Born in", 2002)&lt;/code&gt;, or turn the number into text first with &lt;code&gt;str(2002)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ValueError: invalid literal for int() with base 10
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;int()&lt;/code&gt; only works when the text really is a whole number. If someone types a word, the conversion fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;Your&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Lena&lt;/span&gt;
&lt;span class="nc"&gt;Born&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;nineteen&lt;/span&gt;
&lt;span class="nc"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="s2"&gt;"main.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Born in? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="o"&gt;^^^^^^^^^^^^^^^^^^^^^^^&lt;/span&gt;
&lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;literal&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'nineteen'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one isn't a mistake in your code: it's input your program didn't expect. Handling it with &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt; is the topic of Error Handling in Python Course 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to read any TypeError
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Find the line number in the traceback, and the &lt;code&gt;^&lt;/code&gt; mark under the line.&lt;/li&gt;
&lt;li&gt;Look at the operator or function it points to: &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;len()&lt;/code&gt; and so on.&lt;/li&gt;
&lt;li&gt;Read the two types the message names. If you're not sure which value is which, print &lt;code&gt;type()&lt;/code&gt; of each.&lt;/li&gt;
&lt;li&gt;Convert one side so the types match: &lt;code&gt;int()&lt;/code&gt;, &lt;code&gt;float()&lt;/code&gt; or &lt;code&gt;str()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This exact error is part of our free Python trial, "Your Life in Numbers". When it happens, the tutor doesn't tell you to add &lt;code&gt;int()&lt;/code&gt;. It first asks you to read the last line and say which two types it mentions, and only gives the next hint if you need it, so the fix is one you found yourself.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://craftstackacademy.com/blog/python-typeerror-unsupported-operand-int-and-str" rel="noopener noreferrer"&gt;Craftstack Academy&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>debugging</category>
      <category>programming</category>
    </item>
    <item>
      <title>SQLite error "no such column": usually missing quotes, plus 4 other causes</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Sun, 08 Nov 2026 22:10:10 +0000</pubDate>
      <link>https://dev.to/cstackacademy/sqlite-error-no-such-column-usually-missing-quotes-plus-4-other-causes-anj</link>
      <guid>https://dev.to/cstackacademy/sqlite-error-no-such-column-usually-missing-quotes-plus-4-other-causes-anj</guid>
      <description>&lt;p&gt;You want the visitors who went into the room called Vault, so you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vault&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and SQLite answers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: Vault
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;Vault&lt;/code&gt; isn't supposed to be a column. It's the value you're looking for. That's exactly the problem, and the fix is two small marks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;Here's a tiny table to run in any SQLite tool, such as DB Browser for SQLite or the &lt;code&gt;sqlite3&lt;/code&gt; command-line shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ava Brooks'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Lobby'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'21:40'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ben Carter'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'22:15'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cleo Diaz'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'22:50'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Dan Evans'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Cafe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'23:05'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why SQLite looks for a column
&lt;/h2&gt;

&lt;p&gt;Apart from SQL's own keywords like &lt;code&gt;SELECT&lt;/code&gt; and &lt;code&gt;WHERE&lt;/code&gt;, a word without quotes is a name: the name of a table, a column or a function. So in &lt;code&gt;WHERE room = Vault&lt;/code&gt;, SQLite reads &lt;code&gt;Vault&lt;/code&gt; the same way it reads &lt;code&gt;room&lt;/code&gt;, as a column, and compares the two columns. There's no column called &lt;code&gt;Vault&lt;/code&gt;, so it stops.&lt;/p&gt;

&lt;p&gt;Text values, the actual words stored in the table, go in single quotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ben Carter
Cleo Diaz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Numbers don't need quotes (&lt;code&gt;WHERE id = 2&lt;/code&gt;), and neither do column names. Text always does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about double quotes?&lt;/strong&gt; SQLite often accepts &lt;code&gt;"Vault"&lt;/code&gt; too, but only as a fallback: double quotes are meant for names, and other databases such as PostgreSQL read &lt;code&gt;"Vault"&lt;/code&gt; as a column. Use single quotes for text and your queries will work everywhere.&lt;/p&gt;

&lt;p&gt;The tool you use may word the start of the message differently. Python's &lt;code&gt;sqlite3&lt;/code&gt; module, for example, raises &lt;code&gt;sqlite3.OperationalError: no such column: Vault&lt;/code&gt;. The important part is the same: &lt;code&gt;no such column&lt;/code&gt;, followed by the word SQLite couldn't find.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four other causes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A typo in a column name
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;nmae&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: nmae
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The word at the end of the message is your clue: it's exactly what you typed. Compare it with the real column names letter by letter.&lt;/p&gt;

&lt;h3&gt;
  
  
  A column that's in another table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coat&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: coat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe &lt;code&gt;coat&lt;/code&gt; exists, but in a different table. A query can only use columns from the tables in its &lt;code&gt;FROM&lt;/code&gt; part. To use columns from two tables, you join them with &lt;code&gt;JOIN&lt;/code&gt;, which is a topic of its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  A table alias that wasn't declared
&lt;/h3&gt;

&lt;p&gt;Short names for tables, like &lt;code&gt;v&lt;/code&gt; for &lt;code&gt;visitors&lt;/code&gt;, only exist if you declare them in the &lt;code&gt;FROM&lt;/code&gt; part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: v.name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the alias after the table name, and use it consistently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A column added after the table was created
&lt;/h3&gt;

&lt;p&gt;This one catches people who build a small app. The code starts with &lt;code&gt;CREATE TABLE IF NOT EXISTS&lt;/code&gt;, and later you add a column to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the database file already has a &lt;code&gt;visitors&lt;/code&gt; table from before, without &lt;code&gt;entered&lt;/code&gt;, this line does nothing: the table exists, so it isn't created again. The next &lt;code&gt;SELECT entered FROM visitors&lt;/code&gt; fails with &lt;code&gt;no such column: entered&lt;/code&gt;, even though the column is right there in your code.&lt;/p&gt;

&lt;p&gt;Add the column to the existing table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, while you're still practising and the data doesn't matter, delete the database file (or &lt;code&gt;DROP TABLE visitors;&lt;/code&gt;) and let the code create it again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors that look alike
&lt;/h2&gt;

&lt;p&gt;A few other messages come from the same kind of mix-up between names and values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;no such table: visitor&lt;/code&gt;&lt;/strong&gt;: the same problem one level up, a misspelled table name (here &lt;code&gt;visitor&lt;/code&gt; instead of &lt;code&gt;visitors&lt;/code&gt;), or a table that was never created in this database file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;table visitors has no column named nmae&lt;/code&gt;&lt;/strong&gt;: what SQLite says when the misspelled column is in an &lt;code&gt;INSERT&lt;/code&gt;, like &lt;code&gt;INSERT INTO visitors (nmae, room) VALUES ('Eve Fox', 'Lobby');&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ambiguous column name: name&lt;/code&gt;&lt;/strong&gt;: the opposite problem. After a &lt;code&gt;JOIN&lt;/code&gt;, two tables both have a column called &lt;code&gt;name&lt;/code&gt;, and SQLite doesn't know which one you mean. Put the table name in front: &lt;code&gt;SELECT visitors.name, rooms.floor FROM visitors JOIN rooms ON visitors.room = rooms.name;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you start counting and grouping rows, a different family of messages appears: &lt;a href="https://craftstackacademy.com/blog/sql-group-by-misuse-of-aggregate-must-appear-in-group-by" rel="noopener noreferrer"&gt;SQL GROUP BY errors: "misuse of aggregate" and "must appear in the GROUP BY clause"&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same error in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;If you move to PostgreSQL, the missing quotes give &lt;code&gt;ERROR: column "vault" does not exist&lt;/code&gt;. Notice the lower-case &lt;code&gt;vault&lt;/code&gt;: PostgreSQL turns names without quotes into lower case, so the message doesn't even show the word exactly as you typed it. And &lt;code&gt;"Vault"&lt;/code&gt; in double quotes, which SQLite quietly accepts as text, is a column name in PostgreSQL: &lt;code&gt;ERROR: column "Vault" does not exist&lt;/code&gt;. Single quotes work in both.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to see which columns a table has
&lt;/h2&gt;

&lt;p&gt;In SQLite, &lt;code&gt;PRAGMA table_info&lt;/code&gt; lists the columns of a table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;table_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visitors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;cid&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;type&lt;/th&gt;
&lt;th&gt;notnull&lt;/th&gt;
&lt;th&gt;dflt_value&lt;/th&gt;
&lt;th&gt;pk&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;id&lt;/td&gt;
&lt;td&gt;INTEGER&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;name&lt;/td&gt;
&lt;td&gt;TEXT&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;room&lt;/td&gt;
&lt;td&gt;TEXT&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;entered&lt;/td&gt;
&lt;td&gt;TEXT&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most tools also show the columns in a sidebar. Check the list before you go hunting for anything more complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Read the word at the end of the message: that's what SQLite tried to use as a column.&lt;/li&gt;
&lt;li&gt;Is it a value, like a room, a name or a color? Put it in single quotes.&lt;/li&gt;
&lt;li&gt;Is it meant to be a column? Check the spelling against the table's columns with &lt;code&gt;PRAGMA table_info&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Is the column in another table? Add that table with a &lt;code&gt;JOIN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Does it start with a short name like &lt;code&gt;v.&lt;/code&gt;? Declare that alias in the &lt;code&gt;FROM&lt;/code&gt; part.&lt;/li&gt;
&lt;li&gt;Did you add the column to your code later? Add it to the existing table with &lt;code&gt;ALTER TABLE ... ADD COLUMN&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once it works, you can keep adding conditions with &lt;code&gt;AND&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Vault'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;entered&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'22:30'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cleo Diaz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same "read the last word" habit works for errors in other languages too: &lt;a href="https://craftstack.hashnode.dev/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql" rel="noopener noreferrer"&gt;How to Read an Error Message: A 3-Step Method for Python, Java, JavaScript and SQL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;New to SQL and wondering how it compares with other languages? See &lt;a href="https://craftstackacademy.com/blog/which-programming-language-to-learn-first" rel="noopener noreferrer"&gt;Which programming language should you learn first?&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article was first published on the &lt;a href="https://craftstackacademy.com/blog/sqlite-no-such-column-error" rel="noopener noreferrer"&gt;Craftstack Academy blog&lt;/a&gt;. It was written with the help of AI, and every example was checked in SQLite.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>database</category>
      <category>debugging</category>
    </item>
    <item>
      <title>JavaScript: Cannot read properties of null (reading 'addEventListener')</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Sat, 07 Nov 2026 16:30:24 +0000</pubDate>
      <link>https://dev.to/cstackacademy/javascript-cannot-read-properties-of-null-reading-addeventlistener-21c2</link>
      <guid>https://dev.to/cstackacademy/javascript-cannot-read-properties-of-null-reading-addeventlistener-21c2</guid>
      <description>&lt;p&gt;You added a button and a few lines of JavaScript to make it do something. You click it, nothing happens, and the browser console says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message sounds technical, but it says something simple: JavaScript didn't find your element. Here's how to read it, the mistakes that cause it almost every time, and a few related errors that look alike.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the error means
&lt;/h2&gt;

&lt;p&gt;Read it from the end:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(reading 'addEventListener')&lt;/code&gt;: your code tried to use &lt;code&gt;addEventListener&lt;/code&gt; on something.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;of null&lt;/code&gt;: that something was &lt;code&gt;null&lt;/code&gt;, which means "nothing".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Cannot read properties&lt;/code&gt;: you can't call anything on nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the variable you called &lt;code&gt;addEventListener&lt;/code&gt; on is &lt;code&gt;null&lt;/code&gt;. Where did it come from? Almost always from a line like this, just above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;document.querySelector&lt;/code&gt; gives back the first element that matches the selector, or &lt;code&gt;null&lt;/code&gt; if nothing on the page matches. Then the next line fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error also stops your script at that line, so nothing written after it runs either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: a missing # in the selector
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;querySelector&lt;/code&gt; uses CSS selectors. An id needs a &lt;code&gt;#&lt;/code&gt; in front, and a class needs a &lt;code&gt;.&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"color-btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Change color&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the &lt;code&gt;#&lt;/code&gt;, &lt;code&gt;"color-btn"&lt;/code&gt; means "an element whose tag name is color-btn", like &lt;code&gt;&amp;lt;color-btn&amp;gt;&lt;/code&gt;. There's no such tag on the page, so you get &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fix is one character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or use &lt;code&gt;getElementById&lt;/code&gt;, which takes the id without the &lt;code&gt;#&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the spelling too: &lt;code&gt;color-btn&lt;/code&gt; in the HTML and &lt;code&gt;colour-btn&lt;/code&gt; in the JavaScript are different ids, and the result is the same error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: the script runs before the element exists
&lt;/h2&gt;

&lt;p&gt;This version has a perfect selector and still fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"app.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"color-btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Change color&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser reads the page from top to bottom. It reaches the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and runs &lt;code&gt;app.js&lt;/code&gt; right away, before it has read the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;. At that moment the button doesn't exist yet, so &lt;code&gt;querySelector&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You have three ways to fix it. Any one is enough:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; to the end of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, just before &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Keep it in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; but add &lt;code&gt;defer&lt;/code&gt;: &lt;code&gt;&amp;lt;script src="app.js" defer&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;. The browser then runs it after the page has been read.&lt;/li&gt;
&lt;li&gt;Wrap your code so it waits for the page:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cause 3: an id that's really a class
&lt;/h2&gt;

&lt;p&gt;The selector has to use the same kind of name as the HTML. Here the button has a class, but the selector looks for an id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"color-btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Change color&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;#color-btn&lt;/code&gt; only matches &lt;code&gt;id="color-btn"&lt;/code&gt;, so &lt;code&gt;querySelector&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt; and you get the same error. Match the selector to the HTML: &lt;code&gt;".color-btn"&lt;/code&gt; for a class, &lt;code&gt;"#color-btn"&lt;/code&gt; for an id. And remember that &lt;code&gt;getElementById&lt;/code&gt; only finds ids: it never finds an element by its class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: one script, several pages
&lt;/h2&gt;

&lt;p&gt;Your site has a home page with the button and an About page without it, and both load the same &lt;code&gt;app.js&lt;/code&gt;. On the home page everything works. On the About page, the console shows the error, and anything after that line in &lt;code&gt;app.js&lt;/code&gt; doesn't run there either.&lt;/p&gt;

&lt;p&gt;The script isn't wrong; the element isn't on every page. Only add the listener when the element exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#color-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, shorter, with the &lt;code&gt;?.&lt;/code&gt; operator, which skips the call when the value is &lt;code&gt;null&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lightyellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this only when the element is really optional. If it should be on the page, a silent &lt;code&gt;?.&lt;/code&gt; hides the real problem: fix the selector or the script's position instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors that look alike
&lt;/h2&gt;

&lt;h3&gt;
  
  
  addEventListener is not a function
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;getElementsByClassName&lt;/code&gt; and &lt;code&gt;querySelectorAll&lt;/code&gt; don't return one element, but a list of all matches. A list has no &lt;code&gt;addEventListener&lt;/code&gt;, so this fails with a different message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TypeError: buttons.addEventListener is not a function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the listener to each element in the list instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cannot read properties of undefined (reading 'addEventListener')
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;undefined&lt;/code&gt; instead of &lt;code&gt;null&lt;/code&gt; usually means you took an item from a list that was empty: &lt;code&gt;document.getElementsByClassName("btn")[0]&lt;/code&gt; gives &lt;code&gt;undefined&lt;/code&gt; when there's no element with that class. The causes and fixes are the same as above: check the class name and where the script runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cannot set properties of null (setting 'textContent')
&lt;/h3&gt;

&lt;p&gt;If you assign to the element instead of calling a method on it, like &lt;code&gt;document.querySelector("#total").textContent = "5";&lt;/code&gt;, Chrome says &lt;code&gt;Cannot set properties of null (setting 'textContent')&lt;/code&gt;. It's the same problem: the element wasn't found.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Firefox and Safari
&lt;/h3&gt;

&lt;p&gt;Other browsers word it differently. Firefox says something like &lt;code&gt;button is null&lt;/code&gt;, and Safari something like &lt;code&gt;null is not an object (evaluating 'button.addEventListener')&lt;/code&gt;. Whatever the wording, it means the same thing: the value before the dot is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the problem is the opposite one, your styles not showing up instead of your script failing, see &lt;a href="https://craftstackacademy.com/blog/css-not-working-why-styles-dont-apply" rel="noopener noreferrer"&gt;CSS not working? 7 reasons your styles don't apply&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find the culprit quickly
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open the console (F12 in most browsers, then the &lt;strong&gt;Console&lt;/strong&gt; tab) and click the file name and line number next to the error. It takes you to the line that failed.&lt;/li&gt;
&lt;li&gt;Look at the variable before the dot, here &lt;code&gt;button&lt;/code&gt;, and find the line that created it.&lt;/li&gt;
&lt;li&gt;Log it: &lt;code&gt;console.log(button);&lt;/code&gt;. If the console shows &lt;code&gt;null&lt;/code&gt;, the selector found nothing.&lt;/li&gt;
&lt;li&gt;Compare the selector with the HTML: the &lt;code&gt;#&lt;/code&gt; or &lt;code&gt;.&lt;/code&gt;, the spelling, and whether the element is above or below your script.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The same pattern explains its cousins: &lt;code&gt;(reading 'textContent')&lt;/code&gt;, &lt;code&gt;(reading 'value')&lt;/code&gt; or &lt;code&gt;(reading 'appendChild')&lt;/code&gt; all mean that the element before the dot is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same three questions (what went wrong, where, and what it asks about your code) work for errors in other languages too: &lt;a href="https://craftstack.hashnode.dev/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql" rel="noopener noreferrer"&gt;How to Read an Error Message: A 3-Step Method for Python, Java, JavaScript and SQL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We checked every example in this article in Chrome, which writes the messages exactly as shown. If you're just starting with JavaScript, our guide &lt;a href="https://craftstackacademy.com/blog/which-programming-language-to-learn-first" rel="noopener noreferrer"&gt;Which programming language should you learn first?&lt;/a&gt; explains where it fits next to HTML and CSS.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article was first published on the &lt;a href="https://craftstackacademy.com/blog/javascript-cannot-read-properties-of-null-addeventlistener" rel="noopener noreferrer"&gt;Craftstack Academy blog&lt;/a&gt;. It was written with the help of AI, and every example was checked in Chrome.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>debugging</category>
    </item>
    <item>
      <title>How to Read an Error Message: A 3-Step Method for Python, Java, JavaScript and SQL</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:16:49 +0000</pubDate>
      <link>https://dev.to/cstackacademy/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql-3f5b</link>
      <guid>https://dev.to/cstackacademy/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql-3f5b</guid>
      <description>&lt;p&gt;Every beginner meets the same moment: you press Run, the screen fills with red text, and your first instinct is to close it and stare at your code instead.&lt;/p&gt;

&lt;p&gt;Don't. The error message is the most useful thing on your screen. It was written by the people who built the language, and it usually tells you &lt;strong&gt;what&lt;/strong&gt; went wrong and &lt;strong&gt;where&lt;/strong&gt;. You just need to know where to look.&lt;/p&gt;

&lt;p&gt;Here's a 3-step method that works the same way in Python, Java, JavaScript and SQL, with the most common beginner errors in each language as examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 3-step method
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the last line first.&lt;/strong&gt; That's the error type and the message: the &lt;em&gt;what&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find the line number.&lt;/strong&gt; That's the &lt;em&gt;where&lt;/em&gt;. Look at that line, and if nothing is wrong there, look at the line just above it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Translate the message into a question about your code.&lt;/strong&gt; "unsupported operand types &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;str&lt;/code&gt;" becomes: &lt;em&gt;"Which value on this line is text when I expected a number?"&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it. Let's try it on real errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python: the error is at the bottom
&lt;/h2&gt;

&lt;p&gt;Here's a tiny program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your name? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Born in? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- you are&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python answers with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traceback (most recent call last):
  File "life.py", line 3, in &amp;lt;module&amp;gt;
    age = 2026 - year
          ~~~~~^~~~~~
TypeError: unsupported operand type(s) for -: 'int' and 'str'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1, the last line:&lt;/strong&gt; &lt;code&gt;TypeError: unsupported operand type(s) for -: 'int' and 'str'&lt;/code&gt;. You tried to subtract, and one side is a number (&lt;code&gt;int&lt;/code&gt;) while the other is text (&lt;code&gt;str&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2, the line number:&lt;/strong&gt; line 3, and newer Python versions even underline the exact spot with &lt;code&gt;^&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3, the question:&lt;/strong&gt; &lt;em&gt;which value is text?&lt;/em&gt; &lt;code&gt;year&lt;/code&gt;, because &lt;code&gt;input()&lt;/code&gt; always gives you back text, even when someone types 1990. Convert it first: &lt;code&gt;int(year)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;👉 Full explanation, plus the other ways this error appears: &lt;a href="https://craftstackacademy.com/blog/python-typeerror-unsupported-operand-int-and-str" rel="noopener noreferrer"&gt;Python TypeError: unsupported operand type(s) for 'int' and 'str'&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python also has errors that stop the program before it even starts, like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  File "loop.py", line 3
    print(s)
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same method: the last line says &lt;em&gt;what&lt;/em&gt; (Python expected an indented block), and the message even tells you &lt;em&gt;where&lt;/em&gt; the problem started: the &lt;code&gt;for&lt;/code&gt; on line 2. The line after it needs to be indented.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/python-indentationerror-expected-an-indented-block" rel="noopener noreferrer"&gt;Python IndentationError: expected an indented block after 'if', 'for' or a function&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Java: the compiler stops you before you run
&lt;/h2&gt;

&lt;p&gt;Java checks your code before it runs, so many errors show up at compile time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;java&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nl"&gt;error:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="nl"&gt;types:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                                  &lt;span class="o"&gt;^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;em&gt;incompatible types: String cannot be converted to int&lt;/em&gt;. You're putting text into a box made for whole numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;code&gt;Main.java:6&lt;/code&gt; means file &lt;code&gt;Main.java&lt;/code&gt;, line 6. The &lt;code&gt;^&lt;/code&gt; points at the value that doesn't fit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;em&gt;where does this value come from, and what type is it?&lt;/em&gt; &lt;code&gt;nextLine()&lt;/code&gt; returns a &lt;code&gt;String&lt;/code&gt;. Convert it with &lt;code&gt;Integer.parseInt(...)&lt;/code&gt;, or read a number directly.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/java-incompatible-types-string-cannot-be-converted-to-int" rel="noopener noreferrer"&gt;Java: incompatible types: String cannot be converted to int&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Java's most famous runtime error works the same way. Newer Java versions spell out exactly what was &lt;code&gt;null&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "..." is null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it as a sentence: &lt;em&gt;I couldn't call &lt;code&gt;length()&lt;/code&gt;, because this variable was &lt;code&gt;null&lt;/code&gt;.&lt;/em&gt; The question to ask: &lt;em&gt;where should this variable have been given a value, and why wasn't it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/java-nullpointerexception-cannot-invoke-because-is-null" rel="noopener noreferrer"&gt;Java NullPointerException: Cannot invoke ... because ... is null&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript: open the browser console
&lt;/h2&gt;

&lt;p&gt;In the browser, errors don't pop up on the page. Press &lt;strong&gt;F12&lt;/strong&gt; (or right-click → Inspect) and open the &lt;strong&gt;Console&lt;/strong&gt; tab. One of the most common messages you'll find there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; you tried to use &lt;code&gt;.addEventListener&lt;/code&gt; on something that is &lt;code&gt;null&lt;/code&gt;, meaning &lt;em&gt;nothing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; the console shows the file and line on the right. Click it to jump straight there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;em&gt;why is this element &lt;code&gt;null&lt;/code&gt;?&lt;/em&gt; Usually &lt;code&gt;document.querySelector(...)&lt;/code&gt; didn't find anything: the selector has a typo, or the script runs before the HTML element exists.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/javascript-cannot-read-properties-of-null-addeventlistener" rel="noopener noreferrer"&gt;JavaScript: Cannot read properties of null (reading 'addEventListener')&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL: the last word is your clue
&lt;/h2&gt;

&lt;p&gt;Database errors are short, but they still follow the pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;visitors&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vault&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: Vault
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; SQLite looked for a &lt;em&gt;column&lt;/em&gt; called &lt;code&gt;Vault&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; there's only one line here, so look at the word the message names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;em&gt;is &lt;code&gt;Vault&lt;/code&gt; meant to be a column or a value?&lt;/em&gt; It's a value, the name of a room, so it needs single quotes: &lt;code&gt;WHERE room = 'Vault'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/sqlite-no-such-column-error" rel="noopener noreferrer"&gt;SQLite error "no such column": usually missing quotes, plus 4 other causes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you start counting and grouping rows, you'll meet a different kind of message, about aggregates and &lt;code&gt;GROUP BY&lt;/code&gt;. The same 3 steps work:&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/sql-group-by-misuse-of-aggregate-must-appear-in-group-by" rel="noopener noreferrer"&gt;SQL GROUP BY errors: "misuse of aggregate" and "must appear in the GROUP BY clause"&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  And when there's no error at all?
&lt;/h2&gt;

&lt;p&gt;The hardest bugs are the silent ones. CSS is the classic example: if a style doesn't apply, the browser doesn't complain at all. Then the method changes slightly: instead of reading a message, you &lt;strong&gt;inspect&lt;/strong&gt; the element (F12 → Elements) and check which styles are crossed out and why.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://craftstackacademy.com/blog/css-not-working-why-styles-dont-apply" rel="noopener noreferrer"&gt;CSS not working? 7 reasons your styles don't apply&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same goes for code that runs but gives the wrong answer. No message will save you there. You compare what you &lt;em&gt;expected&lt;/em&gt; with what you &lt;em&gt;got&lt;/em&gt;, and work backwards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Where the "what" is&lt;/th&gt;
&lt;th&gt;Where the "where" is&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Python&lt;/td&gt;
&lt;td&gt;Last line of the traceback&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;File "...", line N&lt;/code&gt; just above it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java&lt;/td&gt;
&lt;td&gt;After &lt;code&gt;error:&lt;/code&gt; or &lt;code&gt;Exception:&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;File.java:N&lt;/code&gt;, or the first &lt;code&gt;at ...&lt;/code&gt; line of the stack trace&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JavaScript&lt;/td&gt;
&lt;td&gt;The red line in the browser Console&lt;/td&gt;
&lt;td&gt;File and line link on the right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL&lt;/td&gt;
&lt;td&gt;The message itself, especially the name it mentions&lt;/td&gt;
&lt;td&gt;The word or clause it names&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Practise reading errors, not just fixing them
&lt;/h2&gt;

&lt;p&gt;The goal isn't to memorise every error. It's to stop being afraid of them. Next time something breaks, before you change a single character, say out loud: &lt;em&gt;what&lt;/em&gt; went wrong, &lt;em&gt;where&lt;/em&gt;, and what &lt;em&gt;question&lt;/em&gt; that raises about your code.&lt;/p&gt;

&lt;p&gt;If you're still choosing a first language, start here: &lt;a href="https://craftstackacademy.com/blog/which-programming-language-to-learn-first" rel="noopener noreferrer"&gt;Which programming language should you learn first? Python, JavaScript, SQL or Java&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article was first published on the &lt;a href="https://craftstackacademy.com/blog/how-to-read-an-error-message" rel="noopener noreferrer"&gt;Craftstack Academy blog&lt;/a&gt;. It was written with the help of AI.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>debugging</category>
      <category>python</category>
    </item>
    <item>
      <title>Why Does 0.1 + 0.2 Not Equal 0.3 in Python?</title>
      <dc:creator>Craftstack Academy</dc:creator>
      <pubDate>Thu, 05 Nov 2026 14:37:31 +0000</pubDate>
      <link>https://dev.to/cstackacademy/why-does-01-02-not-equal-03-in-python-14k</link>
      <guid>https://dev.to/cstackacademy/why-does-01-02-not-equal-03-in-python-14k</guid>
      <description>&lt;p&gt;Open Python and type this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'd expect &lt;code&gt;0.3&lt;/code&gt;. Python gives you this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.30000000000000004
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it gets stranger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're learning to code, this is the moment you start wondering whether you broke something. You didn't. Python is doing exactly what it was designed to do, and so are JavaScript, Java, C# and almost every other language. Try &lt;code&gt;0.1 + 0.2&lt;/code&gt; in your browser's console and you'll get the same &lt;code&gt;0.30000000000000004&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at why, and what to do about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Computers store numbers in binary
&lt;/h2&gt;

&lt;p&gt;You count in base 10: every digit is a number of ones, tens, hundreds, and after the decimal point, tenths, hundredths and so on.&lt;/p&gt;

&lt;p&gt;Computers count in base 2 (binary). After the "binary point" they only have halves, quarters, eighths, sixteenths…&lt;/p&gt;

&lt;p&gt;Some numbers fit perfectly into that system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0.5&lt;/code&gt; is one half → exact&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0.25&lt;/code&gt; is one quarter → exact&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0.75&lt;/code&gt; is a half plus a quarter → exact&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But &lt;code&gt;0.1&lt;/code&gt; (one tenth) can't be built from halves, quarters and eighths. In binary it's an endless repeating pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.0001100110011001100110011...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The same problem you already know: 1/3
&lt;/h2&gt;

&lt;p&gt;You've seen this in base 10. Try writing one third as a decimal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.3333333333...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The threes never stop. At some point you have to cut it off, and the number you wrote down is &lt;em&gt;almost&lt;/em&gt; one third, but not exactly.&lt;/p&gt;

&lt;p&gt;The computer has the same problem with &lt;code&gt;0.1&lt;/code&gt;, just in binary. It has a fixed amount of space for each number (64 bits), so it stores the closest value it can fit. You can see the number Python actually stored:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Decimal&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.1000000000000000055511151231257827021181583404541015625
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;0.1&lt;/code&gt; is really a tiny bit more than 0.1. The same goes for &lt;code&gt;0.2&lt;/code&gt;. Add two slightly-off numbers and the tiny errors add up. That's where the extra &lt;code&gt;...04&lt;/code&gt; comes from.&lt;/p&gt;

&lt;p&gt;It's not only &lt;code&gt;0.1 + 0.2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.7999999999999999
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why &lt;code&gt;print(0.1)&lt;/code&gt; still shows 0.1
&lt;/h2&gt;

&lt;p&gt;If &lt;code&gt;0.1&lt;/code&gt; is stored as &lt;code&gt;0.1000000000000000055...&lt;/code&gt;, why does &lt;code&gt;print(0.1)&lt;/code&gt; show &lt;code&gt;0.1&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Python is being helpful. When it prints a float, it shows the &lt;em&gt;shortest&lt;/em&gt; number that maps back to the same stored value. For &lt;code&gt;0.1&lt;/code&gt;, that's simply &lt;code&gt;0.1&lt;/code&gt;. But after the addition, the result lands on a slightly different stored value, and the shortest way to write that one is &lt;code&gt;0.30000000000000004&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix it
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Round when you display a result
&lt;/h3&gt;

&lt;p&gt;If you only need to &lt;em&gt;show&lt;/em&gt; the number, round it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.3
0.30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Never compare floats with &lt;code&gt;==&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the real lesson. Instead of asking "are these exactly equal?", ask "are these close enough?":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;math.isclose()&lt;/code&gt; lets a tiny difference through, which is exactly what you want when working with decimals.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use &lt;code&gt;Decimal&lt;/code&gt; when every cent matters
&lt;/h3&gt;

&lt;p&gt;For money and other places where you need exact decimal math, use the &lt;code&gt;decimal&lt;/code&gt; module. Pass the numbers in as &lt;strong&gt;strings&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Decimal&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strings matter here. &lt;code&gt;Decimal(0.1)&lt;/code&gt; would copy the already-imprecise float, and you'd be back to &lt;code&gt;0.1000000000000000055...&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  One more surprise: round() isn't magic either
&lt;/h2&gt;

&lt;p&gt;Now that you know the trick, this one makes sense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.675&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2.67
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'd expect &lt;code&gt;2.68&lt;/code&gt;. But &lt;code&gt;2.675&lt;/code&gt; is stored as something like &lt;code&gt;2.67499999...&lt;/code&gt;, so it rounds down. Same cause, different symptom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;What to do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Showing a decimal to a user&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;round(x, 2)&lt;/code&gt; or &lt;code&gt;f"{x:.2f}"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checking if two decimals are equal&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;math.isclose(a, b)&lt;/code&gt;, never &lt;code&gt;==&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Money or exact decimal math&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Decimal("0.1")&lt;/code&gt; with strings&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;Reading this is one thing. The rule really sticks after you hit the bug yourself. Open Python and try:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;print(0.1 + 0.2 == 0.3)&lt;/code&gt;. Predict the answer first.&lt;/li&gt;
&lt;li&gt;Find another pair of decimals that doesn't add up cleanly.&lt;/li&gt;
&lt;li&gt;Fix the comparison with &lt;code&gt;math.isclose()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next time Python shows you an error instead of a strange number, here's how to read it: &lt;a href="https://craftstack.hashnode.dev/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql" rel="noopener noreferrer"&gt;How to Read an Error Message: A 3-Step Method for Python, Java, JavaScript and SQL&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article was first published on the &lt;a href="https://craftstackacademy.com/blog/why-does-0-1-0-2-not-equal-0-3-in-python" rel="noopener noreferrer"&gt;Craftstack Academy blog&lt;/a&gt;. It was written with the help of AI, and every code example was run in Python 3.13 before publishing.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
