[java] Returning null in a method whose signature says return int?

public int pollDecrementHigherKey(int x) {
            int savedKey, savedValue;
            if (this.higherKey(x) == null) {
                return null;  // COMPILE-TIME ERROR
            }
            else if (this.get(this.higherKey(x)) > 1) {        
                savedKey = this.higherKey(x);
                savedValue = this.get(this.higherKey(x)) - 1;
                this.remove(savedKey);
                this.put(savedKey, savedValue);
                return savedKey;
            }
            else {
                savedKey = this.higherKey(x);
                this.remove(savedKey);
                return savedKey;
            }
        }

The method lies within a class that is an extension of TreeMap, if that makes any difference... Any ideas why I can't return null here?

This question is related to java null return

The answer is


Do you realy want to return null ? Something you can do, is maybe initialise savedkey with 0 value and return 0 as a null value. It can be more simple.


The type int is a primitive and it cannot be null, if you want to return null, mark the signature as

public Integer pollDecrementHigherKey(int x) {
    x = 10;

    if (condition) {
        return x; // This is auto-boxing, x will be automatically converted to Integer
    } else if (condition2) {
        return null; // Integer inherits from Object, so it's valid to return null
    } else {
        return new Integer(x); // Create an Integer from the int and then return
    }
    
    return 5; // Also will be autoboxed and converted into Integer
}

int is a primitive data type . It is not a reference variable which can take null values . You need to change the method return type to Integer wrapper class .


Change your return type to java.lang.Integer . This way you can safely return null


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to null

getElementById in React Filter values only if not null using lambda in Java8 Why use Optional.of over Optional.ofNullable? How to resolve TypeError: Cannot convert undefined or null to object Check if returned value is not null and if so assign it, in one line, with one method call How do I assign a null value to a variable in PowerShell? Using COALESCE to handle NULL values in PostgreSQL How to check a Long for null in java Check if AJAX response data is empty/blank/null/undefined/0 Best way to check for "empty or null value"

Examples related to return

Method Call Chaining; returning a pointer vs a reference? How does Python return multiple values from a function? Return multiple values from a function in swift Python Function to test ping Returning string from C function "Missing return statement" within if / for / while Difference between return 1, return 0, return -1 and exit? C# compiler error: "not all code paths return a value" How to return a struct from a function in C++? Print raw string from variable? (not getting the answers)