Writing/Calling Methods


Compilation Errors


Error Message Name:

	Error: not a statement

Example:

	Scanner s = new Scanner(System.in);
	s.nextLine;

Explanation: When calling a method, you must include () even if there are no parameters. Otherwise, the compiler thinks you are trying to use an instance variable or a constant.

Solution: Add () after the method name:

	Scanner s = new Scanner(System.in);
	s.nextLine();

Error Message name:

	Error: cannot find symbol 
	Symbol: method {method name}{parameter name}
	Location: {class name}

Example A:

	Board b = new Board(5, 5);
	b.putPeg(3, 4, 5);

This will produce the following error message:

	Error: cannot find symbol
	symbol : method putPeg(int,int,int)

Example B:

	Board b = new Board(5, 5);
	putPeg(Board.BLACK, 4, 5);

This will produce the following error message:

	Error: cannot find symbol
	Symbol : method putPeg(Color,int,int)

Causes: You are calling a method that is not recognized by the compiler, because of the following four possibilities:

1) You forgot to import the correct Java libraries.

2) You misspelled a method name, or a method does not exist in the class. To look at all the methods belonging to a class, go to the Java Package Documentation. Search for the class that you think has the method in question.

3) You have called the right method, but the type or the order of the argument is different from the method signature. This is the case in Example A. putPeg's signature has 3 parameters: Color, int and int. The compiler returns an error message because it cannot recognize a putPeg method with three int’s as arguments. To resolve this issue, make sure that your arguments match the method signature in terms of the types of variables used, and the order that they are listed. To fix the example A, we could have:

	b.putPeg(Board.BLACK, 4, 5);

4) You are calling a method using the wrong object. This is the case in example B. Recall, to call a method, you need to follow the pattern of {object name}.{method name}. If you only have the method name, then Java thinks that you are calling a method that is defined in "this" class. Since the compiler cannot find the putPeg method in "this" class, we get an error message. To fix this, we would have:

	b.putPeg(Board.BLACK, 4, 5);

Error Message Name:

	Error: cannot return a value from method whose result type is void

Example:

 	public void square(int num)
	{ return num * num;
	}

Cause: You have failed to indicate that the method is returning a value in the method signature. If you use the keyword void in the method signature, Java interprets that the method will not return any value. However, there exists a return statement (which implies that a value is returned) in the method body, causing the error.

Solution: There are two ways to solve this:

1) Change the method signature, so that it has a return type matching the variable type on the return line. In the above example, our new method signature would be: public int square(int num) This signature matches the return type, which is an int.

2) Change the method body, so that nothing is returned.


Error Message Name:

	Error: incompatible types

Example:

	public String getSpecialChar(int num)
	{  String s = "random";
    	   if (num == 5)
           return (s.charAt(5));
           return "Sorry we cannot grab the special character";
	}

Cause: The return type indicated in the method signature does not match with the type returned in the return statement. In the above example, the method signature specified that getSpecialChar should return a String. However, in the first return statement, s.chatAt(5) is a type char.

Solution: Ensure that the return type matches the method signature. In the above example, both return statements should return a String value. To do so, we need to take advantage of the wrapper class Character, and its toString method. Below is one solution:

	public String getSpecialChar(int num)
   	{ String s = "random";
          if (num == 5)
          return (new Character((s.charAt(5)))).toString();
          return "Sorry we cannot grab the special character";
   	}

Error Message Name:

	Error: unreachable statement

Example:

	private int product = 0;
	public int specialOperation(int num)
	{ this.product = ( num – num * num );
  	  return product;
 	  num = 0;
	}

Cause: In Java, a method stops executing after the return line has being reached. In the above example, the compiler will never execute num = 0.

Solution: A solution is to change the order of the statements. Since nothing will be executed after the return line, it is a good idea to place it as the last line of the method. In our sample code, we would have:

	private int product = 0;
	public int specialOperation(int num)
	{ this.product = ( num – num * num );
  	  num = 0;
  	  return product;
	}

This would guarantee that num = 0 is executed before the return statement.


Error Message Name:

	Error: {method name} has private access in {class name}

Example:

Suppose we have:

 	public class Test
	{ private int getNum()
  	  { return 1986;
  	  }
	}

	public class Test2
	{ public static void main(String[] args)
  	  {  test a = new test();
             System.out.println(a.getNum());
  	  }
	}

The compiler displays the following message when we try to call the getNum method

	Error: getNum() has private access in Test

Cause: This has to do with the visibility modifier of the getNum method. Recall, if you are outside of a class, you only have access to the public methods of that class. For this example, Test2 does not have access to the getNum method, since it is a private method.

Solution: If you want getNum to be accessible from outside of the Test class, then you need to change the visibility modifier to public:

	public class Test
	{ public int getNum()
  	  { return 1986;
  	  }
	}

Error Message Name:

	Error: non-static variable {variable name} cannot be referenced from a static context

Example:

	public class Test
	{ private String s = "TEST";

  	  public String returnString()
  	  { return s;
  	  }

  	  public static void main(String[] args)
  	  { this.returnString(); 
  	  }
	} 

Cause: Recall, static methods can access only other static methods or variables. This is because they do not require instantiation, whereas non-static methods and variables need to be instantiated first. Since the main method is static, it cannot access the non-static method returnString, without creating a Test object first.

Solution: You need to create an object in the static method before calling a non-static method. Below is an acceptable solution:

	public class Test
	{ private String s = "TEST";
  
  	  public String returnString()
  	  { return s;
  	  }
  
  	  public static void main(String[] args)
  	  { Test aTest = new Test();
    	    aTest.returnString();
  	  }
	}