15

The following code gives me compile-time errors: missing return value and missing return statement, what value would I return for this Void Type?

final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() throws Exception {
        // some code
        if (something) {
            return;
        }
    }
}
Willi Mentzel
  • 24,988
  • 16
  • 102
  • 110
HericDenis
  • 1,324
  • 12
  • 28
  • 2
    Use return null. See http://stackoverflow.com/questions/643906/uses-for-the-java-void-reference-type – jontro Dec 06 '12 at 18:00
  • I cant use Void in lowercase because of generics – HericDenis Dec 06 '12 at 18:00
  • @HericDenis: Then you should understand that you *do* need to return something (probably a null reference)... it's not just a normal void method with no return value. – Jon Skeet Dec 06 '12 at 18:01
  • I will return null, but I was like: maybe some nullPointerException will happen idk, this is crazy – HericDenis Dec 06 '12 at 18:02

2 Answers2

30

Void is not void, change it to void type if you don't want to return anything.

Void is a class, void is type.

/**
 * The {@code Void} class is an uninstantiable placeholder class to hold a
 * reference to the {@code Class} object representing the Java keyword
 * void.
 *
 * @author  unascribed
 * @since   JDK1.1
 */

If you want Void, then you need to add return statement at end.

Example:

protected Void doInBackground() throws Exception {
    // some code
    if (something) {
       return null;
    }
    return null;
}
Willi Mentzel
  • 24,988
  • 16
  • 102
  • 110
kosa
  • 64,776
  • 13
  • 121
  • 163
1

Check this. It asks to return Void with captital "V" not with simple void

final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() throws Exception {
        // my code here
        return null;
    }
}
Willi Mentzel
  • 24,988
  • 16
  • 102
  • 110
someone
  • 6,419
  • 7
  • 34
  • 57