• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Java] Missing Return Statement

butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Hi guys, I was wondering why this method:
PHP:
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
        DisplayMode goodModes[] = vc.getDisplayModes();
        for(int x = 0; x < modes.length; x++){
            for(int y = 0; y < goodModes.length; y++){
                if(displayModesMatch(modes[x], goodModes[y])){
                    return modes[x];
                }
            }
        }
    }

says: 'Missing Return Statement'

It clearly says 'return modes[x]' right o_O.

Code:
vc is my Graphics Device btw.
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
im not a java person but i think its because its looking for a return in case the code comes out false
 
Newbie Spellweaver
Joined
Nov 27, 2009
Messages
94
Reaction score
58
im not a java person but i think its because its looking for a return in case the code comes out false

Yup, look at your code in the for loop, the return statement is within an if statement. What if that if statement turns out to be false? Then it can't return anything as there is no other code to be read.

It should be

PHP:
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
        DisplayMode goodModes[] = vc.getDisplayModes();
        for(int x = 0; x < modes.length; x++){
            for(int y = 0; y < goodModes.length; y++){
                if(displayModesMatch(modes[x], goodModes[y])){
                    return modes[x];
                }
            }
        }
return null;
    }
 
Junior Spellweaver
Joined
Apr 12, 2009
Messages
104
Reaction score
10
No, return will redirect program flow out of the loop and back to the caller. I use it extensively, for example:
Code:
double eval(ArrayList<Double> in) {
			switch(this) {
				case ADD:
					return in.get(1) + in.get(0);
				case SUB:
					return in.get(1) - in.get(0);
				case MUL:
					return in.get(1) * in.get(0);
				case DIV:
					return in.get(1) / in.get(0);
				case MOD:
					return in.get(1) % in.get(0);
				case POW:
					return Math.pow(in.get(1), in.get(0));
				case SQRT:
					return Math.sqrt(in.get(0));	
				case ABS:
					return Math.abs(in.get(0));
				case TOBOOL:
					return (in.get(0) > 0) ? 1.0 : 0.0;
				default:
					throw new ArithmeticException("Floating point inputs can only be operated on by floating point operators!");				
			}
		}
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Kk thanks guys, I'll try it in a minute =]
--------
Works guys, thanks a million.
 
Last edited:
Back
Top