Incompatible types: void cannot be converted to

We got into this issue when we were trying to assign a null to an integer variable. As soon as we tried it, we got the following error:

Incompatible types: void cannot be converted to

Incompatible types error

We have a summation method that returns null. Inside the main method, we were trying to call summation and keeping the result in an integer. As null can not be converted to an integer.

public class Calculation {
    public static void main(String[] args) {
        int total = summation(1, 1);
    }

    private static void summation(int firstParam, int secondParam) {
        int result = firstParam + secondParam;
    }
}
Share