Repository navigation
Expand file tree
/
Copy pathcommon-errors-en.html
More file actions
12 lines (12 loc) · 6.97 KB
/
Copy pathcommon-errors-en.html
File metadata and controls
12 lines (12 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
<!doctype html><html lang="en"><meta charset="utf-8"><title>Common Java errors</title><style>body{font:14px sans-serif;max-width:1000px;margin:2em auto}main{columns:2;column-gap:2em}article{break-inside:avoid;margin:0 0 1.2em}h2{font-size:1em}pre{white-space:pre-wrap;overflow-wrap:anywhere;font-size:11px}@media print{body{font-size:10pt;margin:0}button{display:none}pre{font-size:8pt}}</style><h1>Common Java errors</h1><button onclick="print()">Print</button><main><article><h2>A semicolon ; is missing here</h2><pre>class Example { void go() { int n = 1</pre><p>Java noticed the gap at this spot. If you cannot see it here, look at the end of the line above: that is often where it is missing.</p><b>Fix</b><pre>class Example { void go() { int n = 1;
} }</pre></article><article><h2>Java does not know a variable named scor</h2><pre>class Example { int score = 1; int go() { return scor; } }</pre><p>Check the spelling (Java is case-sensitive!) and that the variable is declared before this line, in the same { } block or as a field of the class.</p><b>Fix</b><pre>class Example { int score = 1; int go() { return score; } }</pre></article><article><h2>Java does not know a method named og()</h2><pre>class Example { void go() { og(); } }</pre><p>Check the spelling (Java is case-sensitive!) and that the method belongs to this object. The API help lists the methods of the library.</p><b>Fix</b><pre>class Example { void go() { go(); } }</pre></article><article><h2>Java does not know a class named Strng</h2><pre>class Example { Strng text; }</pre><p>Check the spelling: class names start with a capital letter (String, Sprite). If it is a class of your project, its file must be named Strng.java.</p><b>Fix</b><pre>class Example { String text; }</pre></article><article><h2>= stores a value. To compare, write ==</h2><pre>class Example { void go() { int n = 0; if (n = 2) {} } }</pre><p>In a condition, x = 2 tries to store 2 in x. Compare numbers with x == 2 and texts with a.equals(b).</p><b>Fix</b><pre>class Example { void go() { int n = 0; if (n == 2) {} } }</pre></article><article><h2>A return is missing at the end of this method</h2><pre>class Example { int score() { int n = 4; } }</pre><p>The method promises a value, but on some path through it nothing is returned. Make sure every path ends with return and a value.</p><b>Fix</b><pre>class Example { int score() { int n = 4; return n; } }</pre></article><article><h2>The file ends too early: a closing brace } is missing</h2><pre>class Example { void go() {}</pre><p>Every { needs a matching }. Check that each method and the class itself are closed.</p><b>Fix</b><pre>class Example { void go() {} }</pre></article><article><h2>The ( opened on line 1 needs ), but this is }</h2><pre>class Example { void go() { if (true { } } }</pre><p>Match the closing bracket to the opening bracket on line 1. Round (), square [] and curly braces {} have different jobs.</p><b>Fix</b><pre>class Example { void go() { if (true) { } } }</pre></article><article><h2>The [ opened on line 3 needs ], but this is )</h2><pre> int[] a = new int[3);</pre><p>Match the closing bracket to the opening bracket on line 3. Round (), square [] and curly braces {} have different jobs.</p><b>Fix</b><pre>class Example {
void go() {
int[] a = new int[3];
}
}</pre></article><article><h2>This character is missing its closing '</h2><pre>class Example { char c = 'x; }</pre><p>Put one character between two straight single quotes, for example 'x'. Use double quotes for longer text.</p><b>Fix</b><pre>class Example { char c = 'x'; }</pre></article><article><h2>double does not fit into int without losing part of it</h2><pre>class Example { int n = 1.5; }</pre><p>For example 2.7 would become 2. Make the variable a double, or convert on purpose by writing (int) before the value, for example (int) getX().</p><b>Fix</b><pre>class Example { double n = 1.5; }</pre></article><article><h2>Text cannot be used as int</h2><pre>class Example { int n = "42"; }</pre><p>Even "42" is text, not a number. Convert it: Integer.parseInt(text) or Double.parseDouble(text).</p><b>Fix</b><pre>class Example { int n = 42; }</pre></article><article><h2>n has no value yet</h2><pre>class Example { int go() { int n; return n; } }</pre><p>Give it a start value where you declare it, for example int n = 0;</p><b>Fix</b><pre>class Example { int go() { int n = 0; return n; } }</pre></article><article><h2>n already exists here</h2><pre>class Example { void go() { int n = 1; int n = 2; } }</pre><p>Two variables or methods in the same place cannot have the same name. Rename one. To change an existing variable, leave out the type: x = 5; instead of int x = 5;</p><b>Fix</b><pre>class Example { void go() { int n = 1; n = 2; } }</pre></article><article><h2>go needs (int) but gets (nothing)</h2><pre>class Example { void go(int n) { go(); } }</pre><p>Check the number, the order and the types of the values in the brackets.</p><b>Fix</b><pre>class Example { void go(int n) { go(1); } }</pre></article><article><h2>This text is missing its closing "</h2><pre>class Example { String s = "hello; }</pre><p>A text starts and ends with a straight double quote ". It cannot continue on the next line.</p><b>Fix</b><pre>class Example { String s = "hello"; }</pre></article><article><h2>break only works inside a loop or switch</h2><pre>class Example { void go() { break; } }</pre><p>To leave a method early, use return.</p><b>Fix</b><pre>class Example { void go() { for (int i=0;i<3;i++) { break; } } }</pre></article><article><h2>This method is void, so it cannot return a value</h2><pre>class Example { void go() { return 1; } }</pre><p>Remove the value after return, or replace void with the type of the value, for example public int count().</p><b>Fix</b><pre>class Example { int go() { return 1; } }</pre></article><article><h2>A method cannot be inside another method</h2><pre>class Example { void go() { void inner() {} } }</pre><p>Close the method above with } before you start a new one.</p><b>Fix</b><pre>class Example { void go() {} void inner() {} }</pre></article><article><h2>n is final and cannot change</h2><pre>class Example { void go() { final int n=1; n=2; } }</pre><p>Remove final where n is declared if its value should change.</p><b>Fix</b><pre>class Example { void go() { int n=1; n=2; } }</pre></article><article><h2>Write extends instead of exends</h2><pre>public class Welt exends Spielwelt {</pre><p>extends tells Java that Welt inherits from Spielwelt.</p><b>Fix</b><pre>public class Welt extends Spielwelt {
}
class Spielwelt {}</pre></article><article><h2>Write class instead of cass</h2><pre>public cass Welt extends Spielwelt {</pre><p>class starts the declaration of Welt. Write the class name after class.</p><b>Fix</b><pre>public class Welt extends Spielwelt {
}
class Spielwelt {}</pre></article><article><h2>Write public instead of pulic</h2><pre>pulic class Welt extends Spielwelt {</pre><p>public lets other classes use Welt.</p><b>Fix</b><pre>public class Welt extends Spielwelt {
}
class Spielwelt {}</pre></article></main></html>