Prahasith Veluvolu
March 21, 2019
When we were building our automated common error feedback feature on Mimir Classroom, we analyzed millions of stack traces that our students received when completing their coursework on our platform. In this post, you will find the errors we found to be most troubling to students in Java and some tips on how to approach them.
To start off, here is a quick refresher on how to read an error in the Java stack trace.
public class Driver{
public static void main (String[] args){
System.out.println("Hello")
}
}
This error means that you forgot a semicolon in your code. If you look at the full error statement, it will tell you where in your code you should check within a few lines. Remember that all statements in Java must end in a semicolon and elements of Java like loops and conditionals are not considered statements.
public class Driver{
public static void main (String[] args){
int variableOne = 1;
int sum = variableOne + variableTwo;
}
}
This error means that Java is unable to find an identifier that it is trying to work with. It is most often caused by the following:
public class Driver{
public static void main (String[] args){
public int sum(){
}
}
}
This error usually occurs when your code does not follow Java's standard structure. Check the nesting of your variables and ensure that all your { } and ( ) are in the right place..
public class Driver{
public static void main (String[] args){
int variableOne = 1;
}
}
System.out.println("Hello");
This error is usually caused by misplaced { }. All code in Java needs to be contained within a class, interface, or enum. Ensure that all of your code is within the { } of one of those. We often have seen this error when there is an extra } at the end of your code.
public class Driver{
public static void main (String[] args){
int variableOne = 1;
}
This error usually occurs when you are missing a } somewhere in your code. Ensure that for every { you have one } in the right place.
public class Calculator{
public Calculator(){
}
public int sum(int one, int two) {
int sum = one + two;
}
}
This error usually means one of two things:
import java.util.Scanner;
public class Driver{
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
int in = scan.nextInt();
else if( in > 10)
System.out.println("Your input is greater than 10");
else
System.out.println("Your input is less than 5");
}
}
This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn't nested within your if statement.
import java.util.Scanner;
public class Driver{
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
int in = scan.nextInt();
if in > 10)
System.out.println("Your input is greater than 10");
else
System.out.println("Your input is less than 5");
}
}
This error means that you forgot a left or right parenthesis in your code. If you look at the full error statement, it will tell you where in your code you should check. Remember that for every ( you need one ).
This error means that your switch statement isn't properly structured. Ensure that your switch statement contains a variable like this: switch (variable). Also ensure that every case is followed by a colon (:) before defining your case.
public class Calculator{
public Calculator(){
}
public int sum(int one, int two) {
int s = one + two;
return int s;
}
}
This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls. For example: “return int 7;" should be "return 7;"
public class Thing{
int mode;
public Thing(){
mode = 0;
}
public setMode(int in){
mode = in;
}
}
Every Java method requires that you declare what you return even if it is nothing. "public getNothing()" and "public static getNumber()" are both incorrect ways to declare a method.
The correct way to declare these is the following: "public void getNothing()" and "public static int getNumber()"
public class Driver{
public static void main (String[] args){
char a = b';
}
}
This error occurs when you start a character with a single quote mark but don't close with a single quote mark.
public class Driver{
public static void main (String[] args){
System.out.println("Hello World);
}
}
This error occurs when you start a string with a quotation mark but don't close with a second quotation mark. This error can also occur when quotation marks inside strings are not escaped with a backslash.
This error occurs when you use the wrong variable type in an expression. A very common example of this is sending a method an integer when it is expecting a string. To solve this issue, check the types of your variables and how they are interacting with other types. There are also ways to convert variable types.
public class Calculator{
int mode;
public Calculator(){
}
public void setMode(int in);{
mode = in;
}
}
This error commonly occurs when you have a semicolon on your method declaration line. For example "public static void main(String args[]);". You should not have a semicolon there. Ensure that your methods have a body.
public class Calculator{
int mode;
public Calculator(){
}
public void setMode(int in);{
mode = in;
}
}
This error means that you have code that will never be executed. Usually, this is after a break or a return statement.
If you have any other errors that you find your students encountering often, reach out to us at hello@mimirhq.com and let us know!