Understanding Common Errors In Java

Picture of Prahasith Veluvolu

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.

JavaStackTrace

 

 1. ’;’ expected

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.


2. cannot find symbol

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:


  • You misspelled a variable name, class name, or a keyword somewhere. Remember that Java is case sensitive.
  • You are trying to access a variable or class which does not exist or can not be accessed.
  • You forgot to import the right class.

 


3. illegal start of expression

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..


4. class, interface, or enum expected

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.


5. reached end of file while parsing

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.


6. missing return statement

public class Calculator{

	public Calculator(){

	}

	public int sum(int one, int two) {
		int sum = one + two;
	}

} 

 

 

This error usually means one of two things:


  • Your method is expecting a return statement but is missing one. Ensure that if you declared a method that returns something other than void and that it returns the proper variable type.
  • Your return statements are inside conditionals who’s parameters may not be reached. In this case, you will need to add an additional return statement or modify your conditionals.

7. 'else' without 'if'

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.


8. '(' expected or ‘)’ expected

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 ).


9. case, default, or '}' expected

 

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.


10. '.class' expected

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;"


11. invalid method declaration; return type required

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()"


12. unclosed character literal

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.


13. unclosed string literal

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.


14. incompatible types

 

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.


15. missing method body

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.


16. unreachable statement

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!

Create A Free Account