# Adding parent class to arraylist with internal function

**URL:** <https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766>\
**Category:** Coding Questions\
**Created:** [January 23, 2019, 4:37pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766 "2019-01-23T16:37:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 23, 2019, 4:37pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/1 "2019-01-23T16:37:53Z")

</div>

I’m trying to build a game where you connect different pipes to complete a rube-goldberg machine level, this class is meant to be a connector wire between two blocks. I’m having an issue within the internal findend() function which cannot add the class it is inside of into an arraylist, I know I’m doing this wrong, so how would I make this class add itself to an arraylist?

```auto
class connector {
  int inx, iny, nch, pch;
  String parent, child;
  connector aep;
  String[] payload;
  int tx, ty, sty, stx;
  connector(int _inx, int _iny, String _parent, String[] _payload, int _pch) {
    inx=_inx;
    iny=_iny;
    parent=_parent;
    payload=_payload;
    pch=_pch;
  }
  void dline() {
    fill(0);
    if (aep==null) {
      findend();
      line(inx, iny, mouseX, mouseY);
    } else {
      for (floebox i : elements) {
        if (i.id==child) {
          stx=i.spx;
          sty=i.spy+(15+nch*10);
        }
        if (i.id==parent) {
          tx=i.spx;
          ty=i.spy+(15+pch*10);
        }
      }
      line(tx, ty, stx, sty);
    }
    fill(255);
  }
  void findend() {
    if (mousePressed) {
      println("one");
      for (floebox i : elements) {
        println("two");
        for (int z=0; z<i.il; z++) {
          println("three");
          if (5>dist(mouseX, mouseY, i.spx, i.spy+(15+z*10))) {
            println("four");
            i.incon.add(z, this);//returns outofbounds, says that incon is of length zero and im trying to insert at 2
            child=i.id;
            nch=z;
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 23, 2019, 5:04pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/2 "2019-01-23T17:04:12Z")

</div>

How is `incon` defined? Please, either give code cut down to the issue barebones, or all of it! .-.

---

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 23, 2019, 7:17pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/3 "2019-01-23T19:17:10Z")

</div>

```auto
class connector {
  void findend() {
//i is the element accessed from a foreach loop, and incon is supposed to be an arraylist containing container classes
            i.incon.add(z, this);I'm trying to add this class to that arraylist
  }
}

```

Here’s a simplified version

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 23, 2019, 7:20pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/4 "2019-01-23T19:20:59Z")

</div>

We’re still gonna need a definition of what a floebox is so we can know what incon.add() does. When Architector says

> [@Architector\_4](#):
>
> cut down to the issue barebones, or all of it

he means we can’t help you because what you’re doing wrong might not be in the code you’re giving us, but in the implementation of the classes and functions you’ve written instead.

---

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 23, 2019, 10:32pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/5 "2019-01-23T22:32:14Z")

</div>

Sorry, I’m not too good at cutting down code… I didn’t redefine .add, its a built-in java function that allows you to add an element in at a given position, Z is meant to be the variable that stores the position. Anything else?

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 23, 2019, 10:44pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/6 "2019-01-23T22:44:09Z")

</div>

.add isn’t really the questionable part here.

Architector asked

> [@Architector\_4](#):
>
> How is `incon` defined?

and incon appears to be a member of object `i` which appears to be of type `floebox`

> [@Bluu](#):
>
> `for (floebox i : elements) {`

We don’t know what a `floebox` is, how it’s defined, or the type of any of its members.

You mentioned that `.add` is a built in java function but we still don’t know what type of object `incon` is, since `.add` is a very ambiguous name that is used in several built in Java classes.

Please take a look at the [Guidelines—Tips on Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147) page and take special note to the following sections:

- Is your problem isolated?
- Can we run your code to see the same thing as you?

---

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 24, 2019, 1:36am UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/7 "2019-01-24T01:36:13Z")

</div>

I apologize for incoherence, Incon is an arraylist nested in the floebox class, Nothing special, just an empty arraylist defined at the start of a class, like this:

```auto
class floebox(){
[...]
  ArrayList<connector> incon;
  ArrayList<connector> outcon;
  String[] in, out, TEMPDUMMYPAYLOAD;
  int il, ol, spx, spy, swidth, xofs, yofs;
  floebox(blockmd b, int _spx, int _spy, String _id) {
    name=b.name;
    il=b.in.length;
    ol=b.out.length;
    spx=_spx;
    spy=_spy;
    swidth=int(textWidth(name))+50;
    id=_id;
    outcon=new ArrayList<connector>(ol);
    incon=new ArrayList<connector>(il);
  }
[...]
}

```

I’m really sorry about the example code, I write really messy spaghetti code and fix it afterwards, if It dosen’t peeve you too much, just tell me what I leave out

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 24, 2019, 1:47am UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/8 "2019-01-24T01:47:31Z")

</div>

That’s fine. I think this information here should make debugging this a lot easier. I’m a bit busy right now but if nobody picks this up soon I’ll take a look at it again later.

I think what you have here _might_ be enough information to start looking at solutions.

---

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 24, 2019, 3:26pm UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/9 "2019-01-24T15:26:02Z")

</div>

Alright, no rush, I appreciate your kindness

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 25, 2019, 3:51am UTC](https://discourse.processing.org/t/adding-parent-class-to-arraylist-with-internal-function/7766/10 "2019-01-25T03:51:43Z")

</div>

I finally got to looking at your code but when I got everything together, I found an issue with trying to get these classes to understand each other. It seems that the definition of floebox is reliant on the definition of connector, but connector relies on floebox. You have a [circular dependency](https://en.wikipedia.org/wiki/Circular_dependency) in your code.

Also, floebox seems to rely on blockmd which I don’t have access to. `name` inside of floebox’s definition is also never defined, so its purpose cannot be accommodated for.

I’m going to reiterate once again to look at the [Guidelines–Tips on Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147) sections labeled:

- Is your problem isolated?
- Can we run your code to see the same thing as you?

Final note: rereading your question which is specifically regarding the behavior of findend(), which has several issues keeping it from being testable on our end, I can see that your original problem revolved around adding elements to an ArrayList.

I would consider using ArrayList’s [overloaded add() function](https://www.tutorialspoint.com/java/util/arraylist_add.htm) that only takes in one operator: the item you’re adding. You don’t need to use an index to add an ArrayList to the next index.

```auto
i.incon.add(this);

```

It might solve your issue. I can’t be sure it will, as I don’t have enough information on your problem.
