Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[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