no return statement?

Feb 28, 2007 at 8:12pm

no return statement?

I’ve had this problem before in the past and never really figured it out (just re-worked everything to avoid it). In this simple little method,

public int[] noteOn()
{
int[] onEvent = new int[3];
for(int i=0;i < Margin;i++)
{
onEvent[0] = Colours.get(margin).intValue();
onEvent[1] = Pitch;
onEvent[2] = Velocity;
return onEvent;
}
}

I’m getting an error of “missing return statement”. I don’t really understand why — I suppose it’s that there might be a situation in which the return is unreachable…

But I want to return multiple “onEvents”. Is there an easy way around the “missing return” problem?

J.

#30549
Feb 28, 2007 at 8:18pm

hmm… I did this:

public int[] noteOn()
{
int[] onEvent = new int[3];
onEvent[1] = Pitch;
onEvent[2] = Velocity;
for(int i=0;i < Margin;i++)
onEvent[0] = Colours.get(i).intValue();
return onEvent;
}

and it compiled (just re-positioning the for loop and leaving out the brackets!). But I’ve no idea whether it will actually return multiple “onEvent” arrays, one for each “Colour”???

I’ll try it later, when I have the class implemented. But if anyone knows a better way, or can help me understand the “missing return” error better, please post. Is it just that I’ve placed the return in a potentially unreachable spot?

cheers,

J.

#97836
Feb 28, 2007 at 8:21pm

On 07-02-28, at 2012, jbmaxwell wrote:
>
> I’ve had this problem before in the past and never really figured
> it out (just re-worked everything to avoid it). In this simple
> little method,
>
> public int[] noteOn()
> {
> int[] onEvent = new int[3];
> for(int i=0;i < Margin;i++)
> {
> onEvent[0] = Colours.get(margin).intValue();
> onEvent[1] = Pitch;
> onEvent[2] = Velocity;
> return onEvent;
> }
> }
>
> I’m getting an error of “missing return statement”. I don’t really
> understand why — I suppose it’s that there might be a situation in
> which the return is unreachable…
>
> But I want to return multiple “onEvents”. Is there an easy way
> around the “missing return” problem?
>
how big is Margin, and is margin the same? either way, putting a
return statement in your for loop means it may execute the first time
(i == 0), and then return. given that you don’t use i in the body of
your loop, and that it will only execute the loop once and exit, the
whole thing can be rewritten (w/o missing return statement error) as
follows:

public int[] noteOn()
{
int[] onEvent = new int[3];

onEvent[0] = Colours.get(margin).intValue();
onEvent[1] = Pitch;
onEvent[2] = Velocity;

return onEvent;
}

make sense? hope that helps.

r.

#97837
Feb 28, 2007 at 8:28pm

On 07-02-28, at 2018, jbmaxwell wrote:
>
> hmm… I did this:
>
> public int[] noteOn()
> {
> int[] onEvent = new int[3];
> onEvent[1] = Pitch;
> onEvent[2] = Velocity;
> for(int i=0;i < Margin;i++)
> onEvent[0] = Colours.get(i).intValue();
> return onEvent;
> }
>
> and it compiled (just re-positioning the for loop and leaving out
> the brackets!). But I’ve no idea whether it will actually return
> multiple “onEvent” arrays, one for each “Colour”???
>
braces (not brackets) define a code block, and allow you to group
things that are more than one statement long. it’s not incorrect to
put a single statement into braces either, and in many cases makes
the code more readable.

your for loop is now setting the first item in onEvent (i.e., onEvent
[0]) to something Margin number of times. there’s no point in doing
this, you’re just overwriting previous values with the value of
Colours.get(Margin).intValue();

I think what you’re likely really trying to do is return a 2D array:
3 values for each value of Margin. I would recommend reading up on
arrays (and java) to get a better understanding of what you’re trying
to accomplish.

r.

#97838
Feb 28, 2007 at 8:45pm

Right. I get the idea. The point is that I have multiple options for “Colour”, but Pitch and Velocity remain the same. I assume from what you’ve said that my return might not have been reachable, which is why the method wouldn’t compile. Yes? It seems to me I’ve seen return statements in many places other than just the end in plenty of code, but every time I try it I get this error. I suppose all those times I’ve seen it were probably offering alternative return values… not sure. I’d have to find some examples and look more closely at them. Anyway, if the point is simply that I need to have at least one return that’s absolutely reachable, then I can avoid this problem in future.

I can certainly make a 2D array, and I may do that. To be honest, I hadn’t thought that much about it yet. I tried the above code, and it gave me that error I’d seen in the past, so I wanted to get to the bottom of it. I may also just find another way of approaching the whole problem. Not sure, but I’ll work it out. My java is probably a true freak show for any real programmer, but I’ve actually managed to do a lot with it! ;-)

cheers,

J.

#97839
Feb 28, 2007 at 11:40pm

Well first of all the method is declared to return an int array.
If Margin was 0 the for loop would never be entered and hence
there is no way for the method to return anything.

Also , if you were able to compile it, the way it is written you will
always return on the first iteration
through the loop.

So, not understanding exactly what you are trying to do.
Maybe the following will shed some light.

public int[] noteOn()
{
int[] onEvent = new int[3];

onEvent[0] = Colours.get(margin).intValue();
onEvent[1] = Pitch;
onEvent[2] = Velocity;

return onEvent;

}

if you wanted to return multiple events…

public int[][] noteOn()
{
int[][] onEvent = new int[3][];
for(int i =0; i < onEvent.length;i++)
{
onEvent[i][0] = Colours.get(margin).intValue();
onEvent[i][1] = Pitch;
onEvent[i][2] = Velocity;
}

return onEvent;

}

..although i don’t know what this would accomplish since I don’t
have any idea what margin is.

if you can explain what you are trying to do more specifically someone
might be able to better explain…

topher

> public int[] noteOn()
> {
> int[] onEvent = new int[3];
> for(int i=0;i < Margin;i++)
> {
> onEvent[0] = Colours.get(margin).intValue();
> onEvent[1] = Pitch;
> onEvent[2] = Velocity;
> return onEvent;
> }
> }

On Feb 28, 2007, at 20:12 PM, jbmaxwell wrote:

>
> I’ve had this problem before in the past and never really figured
> it out (just re-worked everything to avoid it). In this simple
> little method,
>
> public int[] noteOn()
> {
> int[] onEvent = new int[3];
> for(int i=0;i < Margin;i++)
> {
> onEvent[0] = Colours.get(margin).intValue();
> onEvent[1] = Pitch;
> onEvent[2] = Velocity;
> return onEvent;
> }
> }
>
> I’m getting an error of “missing return statement”. I don’t really
> understand why — I suppose it’s that there might be a situation in
> which the return is unreachable…
>
> But I want to return multiple “onEvents”. Is there an easy way
> around the “missing return” problem?
>
> J.
>

#97840
Mar 1, 2007 at 12:28am

Quote: topher lafata wrote on Wed, 28 February 2007 23:40
—————————————————-
> Well first of all the method is declared to return an int array.
> If Margin was 0 the for loop would never be entered and hence
> there is no way for the method to return anything.
>

Yes, this is what I was thinking when I guessed that the method wouldn’t compile because of the possibility of not reaching the return statement. So that makes sense to me now. Code… it has so little faith! ;-)

> Also , if you were able to compile it, the way it is written you will
> always return on the first iteration
> through the loop.
>

Yes, that’s what I intended. I wanted multiple returns, one for each “Colour”.

> So, not understanding exactly what you are trying to do.
> Maybe the following will shed some light.
>
> public int[] noteOn()
> {
> int[] onEvent = new int[3];
>
> onEvent[0] = Colours.get(margin).intValue();
> onEvent[1] = Pitch;
> onEvent[2] = Velocity;
>
>
> return onEvent;
>
> }
>
>
> if you wanted to return multiple events…
>
> public int[][] noteOn()
> {
> int[][] onEvent = new int[3][];
> for(int i =0; i < onEvent.length;i++)
> {
> onEvent[i][0] = Colours.get(margin).intValue();
> onEvent[i][1] = Pitch;
> onEvent[i][2] = Velocity;
> }
>
> return onEvent;
>
> }

Right. This is the 2D array recommended earlier. That probably makes the most sense. I guess I just didn’t really see it as a 2D array, since it’s really [1 or more] versions of the same array with the first element changing its value…. but of course, that amounts to the same thing. Only I’d have to unpack the 2D array back in my calling class, but that’s not a big deal. The other option is just to return the “Colours” ArrayList with my pitch/vel array, then iterate through the ArrayList back in the calling class. All the info is going pretty much directly to an outlet, so it’s not that big a difference either way.

>
>
> ..although i don’t know what this would accomplish since I don’t
> have any idea what margin is.
>

Margin is just a range of choices for “colours” which are the sounds used to play pitch/vel — in my case they’ll be actual devices playing back the sounds, arranged qualitatively by colour. If the margin is one, I get one device playing the note, if it’s > 1 I get doublings, and so on.

> if you can explain what you are trying to do more specifically someone
> might be able to better explain…
>

I’ll work it out. I was really just curious about the “missing return” error. I understand that now, so I’ll continue gleefully mangling code.

J.

#97841

You must be logged in to reply to this topic.