What does the Java compilation message:
"Incompatible types: void cannot be converted to ..."
mean, and how do I fix it. Some compilers use different wording; e.g.
"Type mismatch: cannot convert from void to ..."
or
"Incompatible types. Required: ... found: void"
or
"Incompatible types: Found void, required ..."
(This is intended to be a canonical Q&A for some very specific compilation error messages involving "void" that tend to confuse new Java programmers. It is not intended to be a tutorial on the various different "type conversion" problems that one can encounter in Java.)
The compiler is telling you that your code is trying to use the "result" of a method that doesn't return a result.
Solution:
Read the javadoc for the method you are trying to call (or the source code if you don't have javadocs).
From the javadocs (or source code), work out how the method should really be used.
Correct your code to either not use the (non-existent) result, or to use a different method. Alternatively, if it is one of your methods that you are calling, another possible correction might be to change the method to return an appropriate value.
Consider this example:
public class Test {
private static void add(int a, int b) {
int res = a + b;
}
public static void main(String[] args) {
int sum = add(1, 1);
}
}
When I compile this using javac
(Java 8), I get the following compilation error.
$ javac Test.java
Test.java:7: error: incompatible types: void cannot be converted to int
int sum = add(1, 1);
^
1 error
The compilation error is actually telling us a few things:
The compiler has detected a problem at the indicated position on the indicated line in the main
method. The root cause of the problem is not necessarily on that line, but that is where the compiler has figured out that something is wrong.
It is a type error - hence the "incompatible types" phrase.
The incompatibility involves two types: void
and int
.
The compiler thinks that the code requires a conversion from void
to int
... and that is not possible.
void
type?Well, you will most likely have learned that Java supports two kinds of type: primitive types and reference types. The void
type is not either of these. It is the "type" that means "there is no value". (If you consider types to be sets of values, then void
is the empty set.)
The primary use for the void
type is used is in method declarations. Look at the declaration of the add
method above. Notice that we have declared add
with the signature:
private static void add(int a, int b)
That signature states that the add
method takes two int
methods, and returns void
. That means that the method will return nothing.
Yet ... we have called it like this:
int sum = add(1, 1);
This is expecting the add
call to return an int
value that we will assign to sum
.
This is what the "void cannot be assigned to ..." error message really means. The compiler is telling us that code is trying to use the result of a method that has been declared to return no result. That's not possible. The Java language does not allow you to "pull a value out of the air" to use.
There are potentially two ways to make the compilation error go away:
We could change the declaration of the method so that it does return a value. For example:
private static int add(int a, int b) {
int res = a + b;
return res;
}
We could change the call site so that it does not attempt yo use the (non-existent) return value. For example:
add(1, 1);
In this example, either approach would do that. But only one approach (the first one) makes the code work as intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With