# Method of custom type not returning?

**URL:** https://discourse.processing.org/t/method-of-custom-type-not-returning/34495
**Category:** Coding Questions
**Created:** [January 7, 2022, 1:45am UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495 "2022-01-07T01:45:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![millibyte](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@millibyte](https://discourse.processing.org/u/millibyte)
#### Post date: [January 7, 2022, 1:45am UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495/1 "2022-01-07T01:45:42Z")

</div>

I’m creating a simple football video game with object oriented programming. One problem I’m having is a method with a return type of a custom object is not returning with said object. More specifically:

I wrote the `Player` class (which corresponds with a football player, not the user) which has an attribute called `carrier`. This attribute is a boolean which is set to false by default. In the main class, I’m writing a method of return type `Player` which iterates through an array of `Player` objects, called `offense`, to find the one where `carrier` is set to true. It looks like this:

```auto
Player getCarrier() {
  for(int i = 0; i < offense.length; i ++) {
    if(offense[i].carrier) {
      return offense[i];
    }
  }
}

```

However, the compiler is saying that the method is not returning an object of type `Player`. What is wrong here?

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [January 7, 2022, 3:21am UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495/2 "2022-01-07T03:21:04Z")

</div>

hi

> **[Reference](https://processing.org/reference/return.html)**
>
> Keyword used to indicate the value to return from a function. The value being returned must be the same datatype as defined in the function declaration. Functions declared with void can't retur…

> **[Arrays](https://processing.org/tutorials/arrays/)**
>
> How to store and access data in array structures.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 7, 2022, 12:31pm UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495/3 "2022-01-07T12:31:25Z")

</div>

The problem here is when _all the players_ in the array are _not carriers_ i.e. `offense[i].carrier` is `false` for all elements in the array. It means the loop will finish without visiting the `return` statement.

The method is required to return an object of type `Player` or subclass of type `Player` for all paths through the method.

So the method should be

```auto
Player getCarrier() {
  for(int i = 0; i < offense.length; i ++) {
    if(offense[i].carrier) {
      return offense[i];
    }
  }
  return null;
}

```

and you would have to test the return value and see if it is null before using it otherwise you will get a NPE (Null Pointer Exception) error

---

<div class="post-metadata">

### Author: ![millibyte](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@millibyte](https://discourse.processing.org/u/millibyte)
#### Post date: [January 7, 2022, 5:57pm UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495/4 "2022-01-07T17:57:34Z")

</div>

Thanks for the help!

---

<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: [January 7, 2022, 7:25pm UTC](https://discourse.processing.org/t/method-of-custom-type-not-returning/34495/5 "2022-01-07T19:25:48Z")

</div>

> [@millibyte](#):
>
> which iterates through an array of `Player` objects, called `offense` , to find the one where `carrier` is set to true.

On the other hand: when you iterate here you search an information (find the one where `carrier` is set to true) that you have stored yourself right before.

So instead of searching the information you could go to the place (ALL places where you set `carrier` to true and additionally store the Player index: carrierPlayerIndex

Then the function would just be:

```auto
Player getCarrier() {
       if(carrierPlayerIndex!=null&&carrierPlayerIndex>-1)
          return offense[carrierPlayerIndex];
      else
         return null; 
}

```
