how can i fix a missing return statement in a loop?

Remove the else from the for loop and put the return statement outside the loop.

public Spherocylinder findSpherocylinder(String labelIn) {
  for (int i = 0; i < spObjects; i++) {
     if (sList[i].getLabel().equalsIgnoreCase(labelIn)) {
        return sList[i];

  return null;

The reason for this is that there might be a case where the for loop may not run even once so the function cannot return anything in that case. Compiler detects this and hence gives an error. It is unlike python where by default function returns a None.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top