# Self made class, not returning when it a loop?

**URL:** <https://discourse.processing.org/t/self-made-class-not-returning-when-it-a-loop/19220>\
**Category:** Beginners\
**Created:** [March 31, 2020, 2:41am UTC](https://discourse.processing.org/t/self-made-class-not-returning-when-it-a-loop/19220 "2020-03-31T02:41:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aldiery](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Aldiery](https://discourse.processing.org/u/Aldiery)\
**Post date:** [March 31, 2020, 2:41am UTC](https://discourse.processing.org/t/self-made-class-not-returning-when-it-a-loop/19220/1 "2020-03-31T02:41:25Z")

</div>

```auto
Ship findShip(int[] coordinates){
  for (int i = 0; i < ships.size();i++){
    Ship ship = ships.get(i);
    int shipX = ship.x;
    int shipY = ship.y;
    for (int tile = 0; tile < ship.size; tile++){
      if (ship.rotated){
        if (shipY == coordinates[1] && shipX + tile == coordinates[0]){
          return ship;
        }
      }
      else{
        if(shipY + tile == coordinates[1] && shipX == coordinates[0]){
          return ship;
        }
      }
    }
  }
}

```

The console returns that the return type must be a Ship, I have been playing with the scope for days and it is killing me  
This method must return a result of type UltimateBattleshipV2.Ship.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 31, 2020, 5:58am UTC](https://discourse.processing.org/t/self-made-class-not-returning-when-it-a-loop/19220/2 "2020-03-31T05:58:20Z")

</div>

You must return a ship also outside any if

At the very end for example

or return null there

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 4, 2020, 2:04am UTC](https://discourse.processing.org/t/self-made-class-not-returning-when-it-a-loop/19220/3 "2020-04-04T02:04:36Z")

</div>

> [@Aldiery](#):
>
> for (int tile = 0; tile \< ship.size; tile++){

This would be a problem even if the for loop guaranteed a match somehow: if ships.size==0, then the for loop will never run, and so findShip will never return anything.

findShip must return something.

So the last line needs to return either `null` or a Ship object (like a special not-a-Ship or default ship. Probably just return null, then handle that when you call findShip – check if what it returned was nothing.
