Extract Method Errors

When you attempt to extract a method, you may get one or more of the following common errors.

Selected block references a local type declared outside the selection

A local type declaration is not part of the selection but is referenced by one of the statements selected for extraction. Either extend the selection that it includes the local type declaration or reduce the selection that no reference to the local type declaration is selected.

A local type declared in the selected block is referenced outside the selection

The selection covers a local type declaration but the type is also referenced outside the selected statements. Either extend the selection that is includes all references to the local type or reduce the selection that the local type declaration isn't selected.

Ambiguous return value: selected block contains more than one assignment to local variable

More than one assignment to a local variable was found inside the selected block. Either reduce the selection that only one assignment is selected or extend the selection that at least all reference except of one to the local variables are covered by the selection too.

Ambiguous return value: more than one reference to selected local declaration found

More than one variable declared inside the selection is also reference outside the selection. Either reduce the selection that only one variable declaration is selected or extend the selection that at least all references except of one to the selected variable declarations are covered by the selection too.

Ambiguous return value: assignment to local variable and reference to a selected local declaration found

The selected block contains an assignment to a local variable. Additionally a variable declared inside the selection is also referenced outside the selection. Reduce the selection that either the assignment or the local declaration is selected. Or extend the selection so that either all references to a local variable or all variable declarations are covered by the selection too.

Ambiguous return value: selection contains return statement and a value must be returned from the extracted method

The selected block contains a return statement. Additionally a value must be returned from the extracted method because either a assignment to a local variable or a local declaration is selected too. Either exclude the return statement from the selection or exclude all assignments to local variables and all local type declarations from the selection.

Ambiguous return value: expression has return type and a value must be returned from extracted method

The selected statement is an expression (for example the condition part of an if-then-else statement) which must return a boolean. Additionally a value must be returned from the extracted method because an assignment to a local variable is selected too. To remedy the problem select the whole statement not only the condition part.

Selection contains a break statement but the corresponding break target isn't selected

To remedy the problem either extend the selection to include the break / continue target or reduce the selection that the break / continue statement isn't covered by the selection.

Selection contains a continue statement but the corresponding continue target isn't selected

To remedy the problem either extend the selection to include the break / continue target or reduce the selection that the break / continue statement isn't covered by the selection.

Cannot extract new method from selection. Only statements from the body of a top level method can be extracted

This is a generic error message shown whenever we encounter a problem but don't really know what the problem actual is. Typical scenarios where this error message is shown are:

Selection starts inside a comment

Parts of a comment cannot be extracted. Either extend the selection that it covers the whole comment or reduce the selection that the comment isn't covered at all.

Selection ends inside a comment

Parts of a comment can't be extracted. Either extend the selection that it covers the whole comment or reduce the selection that the comment isn't covered at all.

Cannot extract selection that ends in the middle of a statement

Adjust selection so that it fully covers a set of statements. Statements in the extract method sense are separated by semicolons.

Other Problems

The following problems are fairly self-explanatory.